One, the need for MVC
Separates the code that creates and operates data from the code that expresses the data. JSP separates most of the performance content from the dynamic content. And the servlet handles business logic.
The servlet is responsible for processing the initial request, locating the data, storing the results in the bean, and then forwarding the request to the JSP page to submit the final result
Second, realize MVC
1, separating the business logic, data and presentation layers.
1, define the bean to represent the data.
2, use the servlet to process the request.
3, fill in the bean.
4, the bean is stored in the context of the request, session, or servlet.
5, forward the request to the JSP page.
6, extract the data from the bean.
2, define the bean to represent the data:
Follow the bean specification. Defines a value object: An object that is used only to represent results, with little or no functionality.
3, use the servlet to process the request:
Used to read request information. Without any output, the output is delivered to the JSP page for completion.
4, fill in the bean.
Use data to fill in the Value object bean. These data can be derived from request parameters, database processing results.
5, store the bean in the context of the request, session, or servlet:
This is done by having the JSP page access the data to display the required data.
6, forward the request to the JSP page.
Use the forward method of the RequestDispatcher object.
* If the JSP page only makes sense in the context of the data generated by the servlet, you can place the page in the Web-inf directory.
can only be forwarded to access these pages, while clients cannot access them directly.
* Forward to static resources:
When a request is sent to an HTML page, a GET request is not forwarded directly to a regular HTML page because the request used for forwarding is the same as the original request.
The workaround is to change the HTML page name extension to JSP. The same response is given for both get and post requests.
* Use redirection instead of forwarding:
When to use forward behavior:
The control forwarding is performed entirely on the server. Does not involve any network data streams.
JSP pages are only meaningful in the context of the data generated by the servlet.
When to use sendredirect behavior:
It is more appropriate to use redirection when you are using a session-based data share.
7, extract the data from the bean:
* JSP pages never create objects.
Use the Type property to specify the parent class when using the <jsp:bean> action, rather than using class.
* JSP pages should not modify objects.
<jsp:setBean> action should not be used.