Three association relationships between Java Web Components:
1. Request Forwarding
2.URL redirect
3. Contains
Request Forwarding:
Allows forwarding of requests to other Web Components in the same application.
This technique is commonly used for the servlet process controller of the Web application control layer, which examines the HTTP request data and forwards the request to the appropriate target component, the target build performs a specific request processing operation, and generates the response results.
Add the following code to the JSP page by adding the servlet:
<jap:forward page="hello.jsp"/>
RequestDispatcher rd=request.geRequestDispatcher("hello.jsp");
//Forward to requested URL
rd.forward(request,response);
Request Redirection:
The same HttpServletRequest object cannot be used between the redirected source component and the target component, so shared data within the request scope cannot be shared.
The service () method in the servlet adds the following code:
Response.sendredirect ("url");
Contains:
The source and target components that contain the relationship share a HttpServletRequest object, so they shared the shared data in the request scope.
Add code to the JSP page:
<%@ include file="header.jsp"%>
<%@ include file="main.jsp"%>
<%@ include file="footer.jsp"%>
servlet的service()方法中加入下面代码:
RequestDispatcher rd;
rd=req.getRequestDispatcher("/header.jsp");
rd.include(req,resp);
rd=req.getRequestDispatcher("/main.jsp");
rd.include(req,resp);
rd=req.getRequestDispatcher("/footer.jsp");
rd.include(req,resp);