Objective: l initially understand the MVC mode; l master servlet compilation; l use the MVC mode to complete the logon function. Main Content: l analyzes the problems existing in the JSP + JavaBean mode, and introduces the JSP + JavaBean + servlet Mode and Its Relationship with the MVC mode; l describes servlet compilation, configuration, and operation through simple examples; l uses servlet to control the login function. 1. Is there a problem with the JSP + JavaBean mode? The strength of JSP lies in interacting with people, that is, completing the input and output functions. However, in the JSP + JavaBean mode, JSP not only implements the input and output functions, but also controls the system (receives user requests, calls JavaBean, and then based on the call results, select the interface to respond to the user ). Therefore, in the third phase of JSP development, the control function is separated from JSP and implemented using Servlet to form the JSP + JavaBean + servlet mode. jsp only completes the input and output, the JavaBean completes the processing, while the servlet completes the control. 2. JSP + JavaBean + servletjsp is responsible for input and output, JavaBean is responsible for implementing business logic (functions), and Servlet completes control. This mode is also considered an implementation of the MVC mode. The MVC mode separates the system's business logic, control, and input/output. When developing applications, you can consider a specific part separately to simplify development. V indicates the view, and the user-linked part. M indicates the model, the function is completed, and C indicates the controller. JSP usually acts as a view, JavaBean is a model, and Servlet acts as a controller. 3. What is servletservlet is also a Web Component? in terms of its complete functions, it is the same as JSP. Servlet is a pure Java file and a special Java class. What is the difference between Servlet and JavaBean? All are Java classes, but Serlvet can receive user requests and the client can directly access them. However, JavaBean cannot be directly accessed by the client. It must be called by JSP or other java files (including servlets. 4. Example: helloservlet is a servlet. The complete function is to output a sentence "servlet test !". 1) Compile the package servlet in a file; // The package required for servlet development is import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; public class helloservlet extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) Throw ioexception, servletexception {response. setcontenttype ("text/html; charset = gb2312"); printwriter out = response. getwriter (); out. print ("servlet test! ");} 2) compile C:/program files/Apache Software Foundation/tomcat 6.0/lib/servlet-api.jar to configure to classpath, and then compile. 3) write the configuration in the configuration file web. xml. The description and access method settings are as follows. Declaration: <servlet> <servlet-Name> Hello </servlet-Name> <servlet-class> servlet. helloservlet </servlet-class> </Serlvet> access method settings: <servlet-mapping> <servlet-Name> Hello </servlet-Name> <URL-pattern>/Hello </url-pattern> </servlet-mapping> 5. Test Access: http: // 127.0.0.1: 8080/ch6/hello6. How do I use this mode to implement the login function? Interface interface and response interface do not need to be modified, JavaBean to complete the processing, do not need to change, just use servlet to replace the original login-process.jsp, to complete the control function. 1) The reference code is as follows: Package servlet; import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; import JavaBean. *; public class loginprocess extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {// Step 1: Get user input string username = request. getparameter ("username"); string userpass = request. getparameter ("userpass"); // Step 2: Call Use Javabean user = new user (); User. setusername (username); User. setuserpass (userpass); Boolean B = user. check (); // Step 3: Select an interface to respond to the user string forward; If (B) forward = "success. JSP "; else forward =" failure. JSP "; requestdispatcher RD = request. getrequestdispatcher (forward); Rd. forward (request, response) ;}the above code basically shows the basic functions of Servlet as a controller. 2) Configure <servlet> <servlet-Name> Process </servlet-Name> <servlet-class> servlet. loginprocess </servlet-class> </servlet> <servlet-mapping> <servlet-Name> Process </servlet-Name> <URL-pattern>/Process </url-Pattern> </servlet-mapping> 3) modify the action attribute of the logon interface <% @ page contenttype = "text/html; charset = gb2312 "%> log on to the <br> <form name =" form1 "method =" Post "Action =" process "> User ID: <input type = "text" name = "username"> <br> password: <inpu T type = "password" name = "userpass"> <br> <input type = "Submit" value = "Logon"> <input type = "reset" value = "reset"> </form> <% @ include file = "contact. JSP "%> 7. Access the test run logon interface, enter the information, and then submit. An error is reported, indicating that the request method is not supported. Add the following method to the servlet: Public void dopost (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {doget (request, response);} because the request method on the JSP page is post, therefore, the servlet must provide the dopost method. The method definition is the same as the doget method. Here, the implementation of the method only needs to call the doget method. 8. The main method of servlet init is used for initialization. service method: doget method and dopost method destroy method. 9. After the lifecycle server receives the request, the request information is encapsulated into an httpservletrequest object and an httpservletresponse object. When accessing the servlet for the first time, load the class, create the object, initialize (init method), call the service class method (if it is a GET request, call the doget method, if it is a POST request, call the dopost method ). For subsequent access, directly call the server class method. Call the destroy method to release resources when you detach a servlet or shut down the server. Reference material: Basic tutorial on Java Web Programming