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);
}
}