Write your own web container Tomjetty (iv) static page start

Source: Internet
Author: User
Tags config http request resource socket root directory tomcat

In the previous section, we implemented the HTTP request Header content parsing and printing to the console, so that each component of the HTTP request header was completely exposed to us. This function in IE browser is called HttpWatch plug-in has a similar embodiment, I believe many readers have used it, using HttpWatch to view the Web page request and response log information function to debug the program. As we said earlier, we encapsulate the HTTP request header in the Requestheader class, which has a URL attribute, which we will use to locate the static page on the server side.

A, server-side static page

As a Web server, responding to requests sent by clients is the primary task, how can you design it to respond quickly to requests? We mentioned earlier, the current Web site architecture is commonly used in CMS mode, is the use of static Web technology and Dynamic Web page technology combined to build the site. Web pages that are relatively fixed or have just been browsed by users can use static technology to improve access efficiency. For example, some htm, CSS, JavaScript and so on.

The so-called static page, that is, after the client sends the request, the server receives the request and resolves the URL of the user to access the resource from the request header, then the server retrieves its corresponding static page htm according to the URL, and finally reads the HTM content to the client browser to display in the stream way. During the entire request-response process, the server side does nothing other than do the basic URL locator resources and read the resource content and write back to the client browser. For example: interaction with Database server, logical operation, servlet request forwarding, etc.

Second, static page construction

Everyone should have used tomcat, open its directory to see, there is a folder named WebApps, The folder is placed under our compiled Web project (. class file, which Java relies on to implement Cross-platform), and a subfolder called Root that holds some of the static pages and configuration files for Tomcat. This is the architectural ^_^ we are going to imitate.

1. Create a new WebApps folder in the root directory of the Tomjetty project.

2. In the Tomjetty.config file, add a new configuration information.

Tomjetty.webapps=./webapps

3. Modify the Run () method of the Tomjetty class. The implementation resolves the URL from the HTTP request header, locates the HTM in the static page in the configuration file, and then reads the HTM content and writes it back to the client browser for display.

@Override public void Run () {inputstream in = null;  
    OutputStream out = null;  
    FileInputStream fin = null;  
        try {in = Socket.getinputstream ();//Get the byte stream sent by the client = Socket.getoutputstream ();//Get the byte stream for the server-side response Byte[] B = new byte[1024 * 1024]; Set byte buffer in.read (b); Reads the client byte stream (the byte stream's request header) string txt = new string (b). Trim (); Encapsulates the request header into a string and prepares to give the parser resolution IREQESTHEADERPARSER parser = (ireqestheaderparser) class.forname (T Omjettyutil.getvalue ("Tomjetty.requestheader.class")). newinstance (); Loading the instance of the request header parser from the Tomjetty.config using the factory design pattern Requestheader Header = parser.parse (TXT);  
        Parsing the request header text, using Requestheader to encapsulate its content System.out.println (header); File HTML = new file (Tomjettyutil.getvalue ("Tomjetty.webapps"), Header.geturl ());  
        From the Profile retrieval server static page storage directory, navigate to the static page of servers side fin = new FileInputStream (HTML); byte[] buf = new byte[(int) Html.lenGth ()]; Fin.read (BUF); Read static page content Out.write (BUF);  
    Writes the static page content back to the client browser to display the catch (Exception e) {e.printstacktrace ();  
            Finally {try {if (fin!= null) {fin.close ();  
            } if (out!= null) {out.close ();  
            } if (in!= null) {in.close ();  
            } if (socket!= null) {socket.close (); The catch (IOException e) {}}}

4. Provide some static pages (including Chinese, English, image content htm) stored in the WebApps directory. The completed WebApps directory looks like this:

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.