Response. sendredirect is two different requests that are redirected on the client, and the browser displays the name of the page after the jump. Because it redirects from the client to the second page.
Javascript: Return big (this) "alt =" flowchart "src ="/data/attachment/portal/et2/201207/et34910080648491.jpg ">
When forward is used, because only one request is sent, the property set for the request (setattribute) can still be stored on the next page.
When sendredirect is used, the request attribute cannot be obtained next time because two request requests are sent. However, you can rewrite the URL.
The content is passed in.
The following is an example:
Tian. jsp
<% Request. setattribute ("River", "Redwater ");
Request. setattribute ("country", "daguaiwan ");
%>
<JSP: Forward page = "fengshan. jsp"/>
Donglan. jsp
<% Request. setattribute ("River", "Redwater ");
Request. setattribute ("country", "daguaiwan ");
%>
<A href = http://developer.51cto.com/art/200907/ "fengshan. jsp"> fengshan </a>
Bama. jsp
<% Request. setattribute ("River", "Redwater ");
Request. setattribute ("country", "daguaiwan ");
Response. sendredirect ("fengshan. jsp ");
%>
Fengshan. jsp
<% = Request. getattribute ("River") %> <br/>
<% = Request. getattribute ("country") %>
- Http: // localhost: 8080/forwardredirect/tiane. jsp:
- Redwater
- Daguaiwan
- Http: // localhost: 8080/forwardredirect/donglan. jsp:
- Null
- Null
- Http: // localhost: 8080/forwardredirect/Bama. jsp:
- Null
- Null
Result Analysis: tiane. JSP jumps to fengshan through forward. JSP, the two pages use the same request, so fengshan. JSP can receive tiane. attributes set by JSP through request. And donglan. JSP sends another request through href, Bama. JSP jumps through sendredirect and uses another request. Therefore, fengshan. JSP cannot receive the attributes set by request on these two pages. Jump between JSP and Servlet: jump from JSP to servlet can be used or response. sendredirect (URL), the difference between them has been described above.
Jump from servlet to JSP: getrequestdispatcher's forward (request, response) method. Here we only send a request, and you can accept the attributes set by the request on the next page. 2. response. sendredirect (URL). Two requests are sent here. The attributes set by the request cannot be accepted on the next page.
Example: servlettiane. jsp
- <%request.setAttribute("river","redwater");
- request.setAttribute("country","daguaiwan");
- %>
- <jsp:forward page="/tiane" />
-
- servletbama.jsp
- <%request.setAttribute("river","redwater");
- request.setAttribute("country","daguaiwan");
- response.sendRedirect("/ForwardRedirect/tiane");
- %>
Tiane. Java
- public class Tiane extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // response.sendRedirect("/ForwardRedirect/fengshan.jsp");
- this.getServletConfig().getServletContext()
- .getRequestDispatcher("/fengshan.jsp").forward(request, response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
Configure servlet in Web. xml:
- <servlet>
- <servlet-name>tiane</servlet-name>
- <servlet-class>com.dvnchina.test.Tiane</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>tiane</servlet-name>
- <url-pattern>/tiane</url-pattern>
- </servlet-mapping>