Write your own web container Tomjetty (vi) dynamic page introduction

Source: Internet
Author: User

In the previous section, we have completed the function of the Tomjetty server to handle static page requests. But the server that can only handle static page requests does not meet our requirements, so we will complete the dynamic page request processing for the Tomjetty server in this section.

The so-called dynamic page request, nothing more than the client sent a request URL address or some request parameters submitted to a certain URL address, server-side first received this URL address and retrieve its in the server program corresponding to a processing class (Servlet), The business logic is then executed in the processing class, and the result is eventually forwarded to the appropriate page to display the results in the client browser.

One, dynamic page request processing

For Java, the service class used by Web containers to handle dynamic page requests is essentially a servlet. Next we implement a loginservlet to handle the user login information submitted by the client.

1. Create a new Loginservlet class for processing client-submitted user logon requests.

Package cn.lynn.servlet;  
Import Cn.lynn.tomjetty.HttpServletImpl;  
Import Cn.lynn.tomjetty.Request;  
      
Import Cn.lynn.tomjetty.Response; public class Loginservlet extends Httpservletimpl {@Override public void doget (Request req, Response Res {String username = Req.getparametervalue ("username");//Receive client request parameter String Password = Req.getparamet  
        Ervalue ("password");  if (Username.equals ("Lynnliget") && password.equals ("123456")) {//Perform simple logical judgment Res.out.print ("Hello," + username + "<br>");  
    Output result Information} res.out.close (); @Override public void DoPost (Request req, Response res) {String username = Req.getparame  
        Tervalue ("username");  
        String Password = req.getparametervalue ("password");  if (Username.equals ("Lynnlipost") && password.equals ("123456")) {Res.out.print ("Hello," + username  
    + "<br>");    } res.out.close (); }  
      
}

2. Create a new Web.config file in the WebApps directory to configure the corresponding relationship between the dynamic page request path and the request processing class.

/login=cn.lynn.servlet.loginservlet

3. Create a new Webutil class to read the name of the request-processing class from the Web.config file and generate an instance of the class. The design and usage of the class are basically consistent with the Tomjettyutil class.

Package cn.lynn.tomjetty;  
      
Import Java.io.FileInputStream;  
Import java.io.IOException;  
Import java.util.Properties;  
      
public class Webutil {  
      
    private static properties props = new properties ();  
          
    static {  
        try {  
            props.load (new FileInputStream (".//webapps//web.config"));  
        \ catch (IOException e) {  
            E.printstacktrace ();  
            System.exit (0);  
        }  
    }  
          
    public static string GetValue (String key) {return  
        props.getproperty (key);  
    }  
}

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.