JSP uses servlet as controller to realize MVC mode. Detailed explanation of an example

This paper describes the method of using servlet as controller to realize MVC mode in JSP. Share with you for your reference. The details are as follows:

1、 Objectives:

① Preliminary understanding of MVC mode; ② Master the compilation of servlet; ③ Use MVC mode to complete the login function.

2、 Main contents:

① This paper analyzes the problems existing in JSP + JavaBean mode, and introduces JSP + JavaBean + Servlet mode and its relationship with MVC mode; ② This paper introduces the writing, configuration and operation of servlet through a simple example; ③ Use servlet to complete the control of login function.

1. What are the problems with JSP + JavaBean?

The power of JSP is to interact with people, that is, to complete the function of input and output. However, in the mode of JSP + JavaBean, JSP not only completes the function of input and output, It also completes the control function of the system (receive the user's request, call JavaBean, and then select the interface to respond to the user according to the call result). Therefore, in the third stage of JSP development, the control function is separated from JSP and implemented by servlet to form the mode of JSP + JavaBean + Servlet. JSP only completes input and output, JavaBean completes processing, and servlet completes control.

2、 JSP+JavaBean+Servlet

JSP is responsible for input and output, JavaBeans are responsible for implementing business logic (function), servlet completes control. This mode is usually considered as an implementation of MVC mode. MVC mode separates the business logic, control and input and output of the system. When developing applications, a certain part can be considered separately to simplify development. V represents the view, the part communicating with people, M represents the model, completes the function, C represents the controller. JSP It usually acts as a view, JavaBeans are models, and servlets are controllers.

3. What is a servlet

Servlet is also a web component, which is the same as JSP in terms of completed functions. Servlet is a pure java file and a special Java class. What is the difference between a servlet and a JavaBean? They are all Java classes, but servlet can receive user requests and the client can access them directly. However, JavaBeans cannot be accessed directly by the client and must be called by JSP or other java files (including servlets).

4. Example: helloservlet

The following is a servlet. The completed function is to output a sentence "servlet test!".

1) Documentation

2) Compile

C: / program files / Apache Software Foundation / Tomcat 6.0 / lib / servlet API Jar is configured into the classpath and compiled.

3) Disposition

In the configuration file web XML. It includes two parts: Declaration and access mode setting.

Statement:

Access method settings:

5. Testing

visit: http://127.0.0.1:8080/ch6/hello

6. How to use this mode to realize the login function?

Neither the interface nor the response interface need to be modified. The JavaBean completes the processing and does not need to be changed. It just uses the servlet to replace the original login process JSP to complete the control function.

1) Reference codes are as follows:

The above code basically shows the basic function of servlet as a controller.

2) Disposition

3) Modify the action attribute of the login interface

7. Test run

Access the login screen, enter information, and submit. At this time, an error will be reported, indicating that the request method is not supported. The following methods need to be added to the servlet:

Because the request mode in the JSP page is post, you need to provide the dopost method in the servlet. The definition of the method is the same as doget. The implementation of the method here only needs to call the doget method.

8. Main methods of Servlet

Init method for initialization; Service class methods: doget method, dopost method and destroy method to release resources

9. Life cycle

After receiving the request, the server will encapsulate the request information into HttpServletRequest object and httpservletresponse object. When accessing the servlet for the first time, load the class, create the object, initialize (init method), call the service class method (doget method in case of get request, and dopost method in case of post request). For subsequent access, directly call the server class method. When unloading the servlet or shutting down the server, call the destroy method to release resources.

I hope this article will be helpful to your JSP programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>