Cannot forward a response this is already committed

Source: Internet
Author: User
Tags flush
Exception one: Java.lang.IllegalStateException:Cannot forward a response is already committed
Anomaly two: Illegalstateexception:response already commited
Exception three: Illegalstateexception:getoutputstream () has already been called for this request

Objective:

response is a service-side response to a client request that encapsulates the response header, status code, content (that is, the HTML code eventually to be displayed on the browser, or other data formats),

the server uses a buffer and writes the response header and status code to the buffer before submitting the response to the client, and then Flush All content (flush contains two steps: First send the buffer contents to the client and then empty the buffer). This indicates that the response has been committed (submitted). For The response that is already committed (submitted) on the current page, you can no longer use this response to write anything to the buffer (note: In the JSP, response is a built-in object for a JSP page, So the response.xxx () of the same page is a different method of the same response, as long as one of them has already caused the committed, then other similar calls can cause illegalstateexception exceptions.

For exception One:

Problem Description :

After executing a piece of code, the last execution request.getrequestdispatcher (jump page URL). Forward (Resquest,response) when the page jumps, reported this error

problem reason :

1. Before the Forwar, response had already made a commit to the browser side.

Causes of response commit include:forward, redirect, Flushbuffer

2. The first two reasons refer to the page Jump action before forward (forward, redirect).

to Flushbuffer, each JSP page has a buffer with a default buffer size of 8KB, and if the buffer is filled, the Web server automatically commits the response and then empties the buffer (that is, flush). So when the page jumps again will be reported response has been commit.

Built-in object out related methods

public abstract void Clear () throws Java.io.IOException Clears the contents of the buffer. If the buffer has been refreshed, the clear () method throws the IOException exception
public abstract void Clearbuffer() throws Java.io.IOException Clears the current content in the buffer. The difference between this method and the clear () method is that this method does not throw a IOException exception if the buffer has been refreshed
public abstract void close () throws Java.io.IOException

Flushes the buffer and closes the output stream. Note that we are writing JSP page, you do not need to explicitly call this method, because in the JSP The code generated by the container automatically contains a pair of close () the invocation of the method.

public abstract void Flush() throws Java.io.IOException Flush buffer, two steps: 1, submit response 2. Empty buffer
public int GetBufferSize() Get buffer size, same as response.getbuffersize ()
public abstract int getremaining() Gets the number of unused bytes in the buffer
public boolean Isautoflush() Determine if an Out object is automatically refreshed
<%@ page autoflush= "true"%> <%--Default--%>

Built-in object response related methods

Response.iscommitted () Confirm response has been committed
Response.flushbuffer (); Same with Out.flush

Workaround:

To increase the buffer size:

<%@ page buffer= "10kb"%>

Reference: http://jorton468.blog.163.com/blog/static/72588135201102441617287/

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.