Response details--the use of redirects and the use of getwrite/getoutputstream conflict issues

Source: Internet
Author: User

It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.


1. REDIRECT implementation:

And the next sentence code interchangeable position, the two lines equivalent to Response.sendredirect ("");
    Response.setstatus (302);
    or "location.jsp"
    response.setheader ("Location", "/httpheader/location.jsp");
2. Use of redirects:

In general, try not to use redirection, as it will send two requests to the server, increase the load on the server, and produce two request and response pairs, except for special occasions:

(1) After the user login successfully jump to the first page, if the use of forwarding, then the user can not see the address bar from the home address

(2) When shopping, click on the "Add Shopping cart" button, and then turn to "show the Shopping Cart merchandise page showcar.jsp", if the use of forwarding technology, then on the "showcar.jsp" page if the page is refreshed, then will "add to the shopping cart event at once."

(3) After the form is submitted (the Get and post methods are the same), if you want to jump to a page, if you use forwarding, then go to the page, if you refresh the page, it will cause the form to repeat the submission:

For example, if you refresh the page after submitting the form as post:


After you click Retry:


After submitting the form, if you redirect to the page instead, refreshing the page will only refresh the page without causing the form to be repeatedly submitted

3. Getoutputstream and Getwriter Mutex issues:

These two methods repel each other, and once any one of these methods is invoked, no other method can be invoked.

such as: Expiresservlet:

	public void doget (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
        //Set cache time to 1 hours: The time unit is milliseconds
		 response.setdateheader ("Expires", System.currenttimemillis () +1000*3600);
		 String data = "Abcdefghigk";
		 Response.getwriter (). print (data);
		 Response.getoutputstream (). Write (Data.getbytes ());
		 JSP is the
		 request.getrequestdispatcher ("index.jsp") that also PrintWriter object output. Forward (request, response); Instead of Response.sendredirect ("index.jsp"), the exception will not be thrown
	}
If you access the servlet, it is thrown: Java.lang.IllegalStateException:getOutputStream () has already been called for this response exception


This means that the two methods of the same response object are mutually exclusive, and after invoking either of these methods, no other method can be invoked.

solution : Replace the Request.getrequestdispatcher ("index.jsp"). Forward (request, response) to Response.sendredirect (" Index.jsp "); The exception is not thrown

cause Analysis: Two response objects when redirected instead

4. Other issues:

(1) The Getoutputstream and Getwriter methods are used to obtain the Servletouputstream and PrintWriter objects of output binary data and output text data respectively.
(2) Getoutputstream and getwriter are mutually exclusive, and after invoking either of these methods, no other method can be invoked.
(3) Data written to the Servletoutputstream or PrintWriter object by the servlet program is retrieved from the response by the servlet engine, which is used as the body of the response message. It is then combined with the response status line and each response header to output to the client.
(4) When the Serlvet service method is finished, the servlet engine checks if the Close method has been invoked by the output stream object returned by the Getwriter or Getoutputstream method, and if not, The servlet engine will call the Close method to shut down the output stream object


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.