Differences between Java redirection and request forwarding

Source: Internet
Author: User

Next, let's look at Java MVC (Model View Controller) ---- (JSP + servlet + javabean instance)
You will surely find the content in the previous article. For details, see the Servlet control layer code:



Import java. text. *; import java. util. *; import java. io. *; import javax. servlet. http. *; import javax. servlet. *; import com. bjpowernode. exam. model. *; import com. bjpowernode. exam. manager. *; public class SearchStudentServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest, httpServletResponse response) throws ServletException, IOException {String sBeginDate = request. getParameter ("beginDate"); String sEndDate = request. getParameter ("endDate"); Date beginDate = new Date (); Date endDate = new Date (); try {beginDate = new SimpleDateFormat ("yyyy-MM-dd "). parse (sBeginDate); endDate = new SimpleDateFormat ("yyyy-MM-dd "). parse (sEndDate);} catch (Exception e) {e. printStackTrace ();} StudentManager studentManager = new StudentManagerImpl (); List <Student> studentList = studentManager. findStudentList (beginDate, endDate); // sets the student list to the requet range // request. setAttribute ("student_list", studentList); // forward, which is forwarded on the server and unknown on the client. // request. getRequestDispatcher ("/student_list.jsp "). forward (request, response); // put studentList in session HttpSession session = request. getSession (); session. setAttribute ("student_list", studentList); // redirection, does not share the request // The following syntax error. This "/" indicates port 8080 // response. sendRedirect ("/student_list.jsp"); response. sendRedirect (request. getContextPath () + "/student_list.jsp ");}}

In this example, we tried two methods to tune to the following Jsp:


Implement forwarding:


// Forwarding: the forwarding is forwarded on the server, and the client does not know the request. getRequestDispatcher ("/student_list.jsp"). forward (request, response );


Analysis: request forwarding means that the server transfers the processing right for one request/response to another. For the client, the server only knows the of its earliest request, without knowing the middle of B, even C, D.The transmitted information will not be lost.


Implement redirection:


// Redirection. The request will not be shared. // the following code is incorrect. This "/" indicates the 8080 port response. sendRedirect ("/student_list.jsp"); response. sendRedirect (request. getContextPath () + "/student_list.jsp ");



In-depth (analysis and understanding)


Forwarding process


The customer first sends a request to the server. The server finds a matched servlet and specifies it for execution. After the servlet is executed, it calls the getRequestDispacther () method, forward the request to the specified student_list.jsp. The whole process is completed on the server side and in the same request. Therefore, the servlet and jsp share the same request, all the items put in the servlet can be obtained in student_list. Therefore, student_list can get the getAttribute () result, and getAttribute () is returned to the client after execution. The whole process is a request and a response.


Redirection Process

The client sends a request to the server and the server matches the servlet. This is the same as request forwarding. After the servlet completes processing, the sendRedirect () method is called. This method is the response method. Therefore, after the servlet is processed, the response is displayed. the senRedirect () method immediately returns this response to the client. The response line tells the client that you must send another request to access student_list.jsp. Then, after the client receives this request, send a new request to student_list.jsp. The two requests do not interfere with each other and are independent of each other. In the previous request, anything about setAttribute () cannot be obtained in the subsequent request. It can be seen that there are two requests and two responses in sendRedirect.


Superficial appearance)


Forwarding

After a request is forwarded using RequestDispatcher, the address bar is http: // localhost: 8080/test/TestServlet.
This is really a response to the above analysis. We initially requested a servlet. As for how your server is transferred and how the process is, I didn't know the client, after I send a request, I will wait for a response. Then, if your server is willing to switch to the server, I can't know if I don't care about it. So when the server forwards the request to jsp, it returns the result to the client, and the client does not know whether the result is generated by the servlet I actually accessed or by the next component after the servlet is forwarded.

Redirection

When sendRedirect is used for redirection, the address bar is http: // localhost: 8080/test/student_list.jsp.
At this time, the client has known that the second request is student_list.jsp, and the server has told the client to access student_list.jsp, so the address bar will display the desired access result.


Summary


The forwarding is completed on the server side; the redirection is completed on the client side.
Fast forwarding speed; slow redirection speed

The same request is forwarded, And the redirection is two different requests.

The forwarded code is not executed for the forwarding; the redirected code is executed for the redirection.

The forwarding address bar has not changed; the redirection address bar has changed

The forwarding must be completed on the same server; the redirection can be completed on different servers.


Forward is a jump on the server side, that is, a client request is sent to the server, the server directly transmits the request-related parameter information intact to other jsp or servlet on the server for processing, while sendredirect is a jump on the client, the server will return a response header and a new URL address to the client. The original parameter or information does not exist if the server does not handle it specially, the browser will access the servlet or jsp pointed to by the new URL, which may not be the webservce on the original server.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.