Forward:
<%Pagecontext.setattribute ("pagecontextattr", "Pagecontextattribute"); Request.setattribute ("requestattr", "Requestattribute"); Session.setattribute ("sessionattr", "Sessionattribute"); Application.setattribute ("applicationattr", "Applicationattribute"); %>PageContext:<%=Pagecontext.getattribute ("pagecontextattr")%><BR/>Request:<%=Request.getattribute ("requestattr")%><BR/>session:<%=Session.getattribute ("sessionattr")%><BR/>Application:<%=Application.getattribute ("applicationattr")%><BR/> <% StringServletname= "myelservletpage.jsp"; RequestDispatcher RequestDispatcher=Request.getrequestdispatcher ("/" +servletname); Requestdispatcher.forward (request, response); // 1, redirect"/"represents the root directory of the current Web site//forward: "/"represents the root directory of the current web App. // 2, Forward: A resource that can only be forwarded to the current Web application;//REDIRECT : You can redirect to any resource, including external resources. %>
Redirect:
<%Pagecontext.setattribute ("pagecontextattr", "Pagecontextattribute"); Request.setattribute ("requestattr", "Requestattribute"); Session.setattribute ("sessionattr", "Sessionattribute"); Application.setattribute ("applicationattr", "Applicationattribute"); %>PageContext:<%=Pagecontext.getattribute ("pagecontextattr")%><BR/>Request:<%=Request.getattribute ("requestattr")%><BR/>session:<%=Session.getattribute ("sessionattr")%><BR/>Application:<%=Application.getattribute ("applicationattr")%><BR/> <% // 1, redirect"/"represents the root directory of the current Web site//forward: "/"represents the root directory of the current web App. Response.sendredirect ("myredirectpage.jsp"); // 2, Forward: A resource that can only be forwarded to the current Web application;//REDIRECT : You can redirect to any resource, including external resources. %>
Myelservletpage.jsp and myredirectpage.jsp content are the same:
PageContext:<%=Pagecontext.getattribute ("pagecontextattr") %><BR/>Request:<%=Request.getattribute ("requestattr") %><BR/>session:<%=Session.getattribute ("sessionattr") %><BR/>Application:<%=Application.getattribute ("applicationattr") %><BR/>
When you access the forward page, the result is:
When you visit the redirect page, the result is:
Forwarding differs from orientation:
1, use does not:
Redirect request: 1, call HttpServletRequest's Getrequestdispatcher () method, get the RequestDispatcher object, call Getrequestdispatcher () requires an incoming forwarding address.
2, call the HttpServletRequest forward (Request,response) method.
Forward request: Invokes the Sendredirect () method of the HttpServletResponse object, passing in the Sendredirect () method directly to the destination address to be forwarded.
2. Redirect request: "/" represents the root directory of the current Web site;
Forward request: "/" represents the root directory of the current web App.
3. Redirect request: Can redirect to any resource (including external resources);
Forward Request: A resource that can only be forwarded to the current web app.
4. Redirect request: Only one request was sent. The browser address does not send changes.
Forward request: Two requests were sent. The browser address sends a change.
5. Conversion Request: The destination page can accept the parameter information passed by the page. In the final servlet, the request object and the request in transit are the same object.
Redirect request: In the final servlet, the request object and the requested request in transit are not the same object.
Java Servlet (ix): The difference between a forwarding request and a redirect request