I. MVC requirements
Separate the code used to create and operate data from the code used to express data. JSP separates most of the content from dynamic content. Servlet is used to process business logic.
The servlet processes the initial request, searches for data, stores the result in the bean, and forwards the request to the JSP page to submit the final result.
2. Implement MVC
1. Separate the business logic, data, and presentation layer.
1. Define bean to represent data.
2. Use servlet to process requests.
3. Enter bean.
4. Store the bean in the context of the request, session, or servlet.
5. forward the request to the JSP page.
6. extract data from bean.
2. Define bean to represent data:
Follow bean specifications. Define "value object": an object that is only used to represent the result. It has only a few features or does not.
3. Use servlet to process requests:
Used to read request information. No output is provided. The output is submitted to the JSP page.
4. Enter bean.
Use Data to enter the value object bean. The data can be obtained from request parameters, databases, and other processed results.
5. Store the bean in the context of the request, session, or servlet:
In this way, the JSP page is accessed 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 makes sense only in the context of the data generated by the servlet, you can place the page under the WEB-INF directory.
You can only forward requests to access these pages, but the client cannot directly access these pages.
* Forward to static resources:
When a request is sent to an HTML page, a GET request cannot directly forward a POST request to a conventional HTML page because it uses the same request method as the original request.
The solution is to change the HTML page extension to JSP. The same response is provided for both get and post requests.
* Replace forwarding with redirection:
When to use forward behavior:
The controlled Forwarding is completely performed on the server. Does not involve any network data streams.
The JSP page only makes sense in the context of the data generated by the servlet.
When to use sendredirect:
It is more appropriate to use redirection for session-based data sharing.
7. Extract data from bean:
* The JSP page never creates objects.
Use the type attribute to specify the parent class when using the <JSP: bean> action, instead of using the class.
* Objects cannot be modified on JSP pages.
You should not use the <JSP: setbean> action.