Develop jsp http Server

Source: Internet
Author: User

Through the comparison of various jsp http servers, locate the application direction of the jsp http Server

For enterprises to choose or design HTTP servers, many factors need to be taken into account, including stability, security, execution efficiency, usability, and scalability. The author analyzes and compares popular HTTP servers in the industry according to common indicators, such as table 1.

HTTPServer Support Service Page Operating Platform Security Execution efficiency Ease of use
MS IIS ASP Windows MS Average Average Easy
MS IIS ISAPI Windows MS Average Good Easy
Apache CGI

Windows MS

UNIX, Linux

Good Average Average
Tomcat JSP

Windows MS

UNIX, Linux

Good Good Not easy

Through table 1, we can know that ms iis can only run on MS Windows platform, and because the IIS system design is largely dependent on Windows system, MS Windows has certain vulnerabilities, which leads to a low security performance in the IIS system. However, IIS is easy to install and use and is suitable for Windows users with low security and scalability requirements.

Apache and Tomcat are both developed by the Apache Software Foundation. These two popular HTTP servers on the Internet not only support the MS Windows platform, it also supports all mainstream non-Windows platforms, such as Linux and Various UNIX operating systems, and has higher security performance than IIS. However, for general users, the configuration of Apache and Tomcat is more complex. Therefore, Apache and Tomcat are suitable for professional users who require high system security and scalability.

Another factor to consider is the execution efficiency of jsp http servers. In table 1, both ASP and CGI adopt the real-time call mode, that is, when the client requests the resource, the server immediately interprets and executes the module. After the execution is completed, the module is immediately released, this module must be interpreted and executed each time a request is obtained. Therefore, excessive interaction with the disk system may reduce the execution efficiency of the system.

For ISAPI, the execution module is called as the dynamic link library DLL) module. After the first call, the module will exist in the memory. Later, when receiving client requests, the module is directly called from the memory to execute the module, which is highly efficient. However, this situation may cause code updates. When modifying the local code, you must clear the dynamic link library module from the memory. That is, you must first shut down the server before updating the local code. Otherwise, the old code module is executed in the server memory.

For the Tomcat System, such problems have been avoided. Tomcat loads the first Java class module to the memory. In subsequent calls, it directly calls the execution module from the memory to reduce interaction with the disk system. At the same time, the system automatically checks whether the local code is modified and updates the old class modules in the memory. This not only improves execution efficiency, but also facilitates local code modification.

Based on the above research and analysis, the author intends to use a scalable and secure Java System to implement an HTTP server that supports JSP service pages. Its function basically covers Tomcat, in addition, the CGI support and ease of use are enhanced on the basis.

I. Design Process
1. Build an HTTP server framework

1.1 design ideas

By establishing TCP socket port 80) to provide HTTP Services to the client. Analyzes client requests such as GET and POST requests, establishes mappings between request resources and local resources, and responds to requests.

1.2 design highlights

1) multi-thread support for client requests.
2) analyze client requests.
3) request the ing between resources and local resources and the response of local resources.
4) Analysis and Processing of CGI and JSP similar requests.
5) extended services and special commands.

1.3 preparations before implementation

1) determine the JDK version and download the JDK
Considering the stability of JDK 1.4.2, we consider using JDK 1.4.2 or later. Download the JDK supported by the platform from SUN's website http://java.sun.com/javase/downloads/index.jsp.
2) install JDK and configure the compiling environment
Install JDK and set the java PATH $ (installation_dir)/bin) to the system PATH variable.

1.4 Design implementation

1) create a service socket

 
  1. //Create server socket ServerSocket serv = new ServerSocket(SERVER_PORT); 
    System.out.println("HTTP server(port: " + Integer.toString(SERVER_PORT) + ") 
    running...");  

2) accept client requests and create request processing threads

 
  1. while(true)  
  2. {  
  3. //Accept the client connections   
  4. Socket clnt = serv.accept();  
  5. //Create thread for each client   
  6. HTTPThread HTTPThd = new HTTPThread(clnt, props, ht);  
  7. HTTPThd.start();  
  8. }  

In the above Code, a client request processing system with a multi-threaded architecture is created. You can process multiple client connections in a timely manner.

3) analyze requests

The client processing thread reads the corresponding request content from the client socket and analyzes the request.

 
  1. //Create client socket input stream reader   
  2. m_sin = new BufferedReader(new InputStreamReader(_s.getInputStream() ) );  
  3. ……  
  4. //Get the first line of output from client socket   
  5. request = m_sin.readLine().trim();  
  6. if(request != null)  
  7. {  
  8. //The method is GET   
  9. if(request.startsWith(METHOD_GET) == true)  
  10. {  
  11. parseGetRequest(request);  
  12. }  
  13. //The method is POST   
  14. else if(request.startsWith(METHOD_POST) == true)  
  15. {  
  16. params = m_sin.readLine();  
  17. //Skip the middle lines of POST request   
  18. while( (params != null) && (params.equals("") == false) )  
  19. {  
  20. params = m_sin.readLine();  
  21. }  
  22. //The last line contains those parameters   
  23. params = m_sin.readLine();  
  24. paramsparams = params.trim();  
  25. parsePostRequest(request, params);  
  26. }  
  27. //Close client socket input stream and client socket itself   
  28. m_sin.close();  
  29. m_s.close();  
  30. }  

The first line of the request content shows whether the request method is GET or POST. For a GET request, for example, many cgi get requests), you can directly obtain the requested resource content from the request string. The GET request is in the format of GET <URL> HTTP/1.x. The URL indicates the requested resource content, and 1. X indicates the HTTP Version supported by the client. Currently, there are two standards: 1.0 and 1.1.

For a POST request, in addition to the requested resource content, such as a JSP file), the last line of the request also contains the parameter lines to be used by the request resource. Therefore, the above Code contains the request content that passes through the intermediate part. You only need to obtain the resource content and parameter lines.

4) ing between request resources and local resources

Generally, for the sake of security, it is impossible for the jsp http server to map the local resource path to the path provided by the Service, but to map a local directory to the root directory of the HTTP service resource, this directory is generally called the Service Page root directory ServerPageDir ). When the client requests resource ing to a local resource, it must replace the root directory in the request with the locally mapped directory. For example, the HTTP server maps the local path "/usr/paul. home" to the root directory of the Service Page. The local path resource corresponding to the "/sample.jpg" request from the client is "/usr/paul. home/sample.jpg ".

When the end character of the request content is "/examples, index. jsp and other files.

Particularly, for security and habits, the CGI directory is also mapped to/cgi-bin /), therefore, if the request contains CGI ing, it must also be replaced with the directory where the CGI program is located.

 
  1. //If the request start with /cgi-bin,  
  2. //then need replace /cgi-bin with true CGI directory   
  3. if(fname.startsWith(PATH_SEPARATOR + CGI_BIN_DIR) == true)  
  4. {  
  5. fnamefname = fname.replaceFirst(PATH_SEPARATOR + CGI_BIN_DIR, cgiBinDir);  
  6. }  
  7. else //else, file name need append to server documents directory   
  8. {  
  9. fname = serverPageDir + fname;  
  10. }  
  11. //If request is for directory,  
  12. //then need respond default page in the directory   
  13. if(fname.endsWith(PATH_SEPARATOR) == true)  
  14. {  
  15. fnamefname = fname + defaultPage;  
  16. }  
  1. Rebuild Struts2 JSP pagination
  2. Three methods for displaying Chinese Characters in JSP Web programs
  3. Java JSP uses Google's Translate to develop APIs
  4. Detailed analysis of nine implicit JSP objects
  5. Introduction to JSP JavaBean development and release Environments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.