Recent work is not too busy, here today to java redirection and forwarding a brief summary of the request, hoping to help you.

 

Forwards the request:

request.getRequestDispatcher().forward();

Redirect:

response.sendRedirect();

E.g:

Forwards the request:

request.getRequestDispatcher("/student_list.jsp").forward(request,response);

Redirect:

response.sendRedirect(request.getContextPath + "/student_list.jsp")



Forwarding process : First, the client sends a request to the server, the server matches the Servlet, and specify the execution. When the Servlet executed, it calls getRequestDispacther () method forwards the request to the specified Servlet_list.jsp, the entire process is done on the server side, but also in the same request to complete the inside, so Servlet and jsp Share the same request, which put all the stuff in the Servlet in the student_list.jsp can take out. Thus, student_list.jsp can result getAttribute () out, getAttribute () after executing out of the results returned to the client, the entire process is a request, a response.

Redirection process: the client sends a request to the server, the server matches the Servlet, which will forward the request and the same. After calling the Servlet sendRedirect processed () This method, which is a response method. So, when the Servlet dealt with, see response.sendRedirect () method immediately returns a response to the client end, the response line tells the client that you have to re-send a request to visit student_list.jsp, followed by the client to close after this request, a new request is sent immediately, to request student_list.jsp, interfering in both requests, independently of each other, in front of anything inside request setAttribute () is behind the request which can not be obtained. Thus, in the sendRedirect () there are two requests and two responses.

 

Forward jump is the server, the client is a request to the server, the server request information directly related parameters transferred intact to the server or other jsp Servlet to process. The sendRedirect () is a Jump client, the server returns the client a response header and a new URL address, the original parameter information if the server is not special treatment does not exist, the browser will access the new URL points to Servlet or jsp, this may not be the original webService on the server.

 

to sum up:

       1, forwarding is done on the server side, the redirection takes place in the client;

       2, fast forward, slow redirection;

       3, a forwarding request is the same, twice the redirection request;

       4, forwarding address bar does not change, the redirection address bar changes;

       5, forwarding must be done in the same server, redirection can be accomplished in different servers.

if you want to go fast,go alone,if you want to go far,go together