Serious: Servlet.service () for Servlet JSP threw Exception__js

Source: Internet
Author: User
Serious: Servlet.service () for Servlet JSP threw exception
Already been called Forthis response after searching online, add two words to the end of the JSP file that generates the CAPTCHA
Out.clear ();
out = Pagecontext.pushbody ();
===========================================================================tomcat5 JSP appears Getoutputstream () has Already been called for the cause of this response exception and resolution in the TOMCAT5 under the JSP is generally used in JSP in the output stream (such as output image verification code, file download, etc.), did not properly deal with the reasons for good. The reason for this is that after the JSP is compiled into a servlet in Tomcat, there is a code at the end of the function _jspservice (httpservletrequest request, httpservletresponse response)
Finally {
if
(_jspxfactory!= null)
_jspxfactory.releasepagecontext (_jspx_page_context);

}
This is where the object used in the JSP is freed, and Response.getwriter () is invoked because the method conflicts with Response.getoutputstream (). So this anomaly will appear. The method is simple. After using the OutputStream output stream, call the following 2 methods to resolve the problem:
Out.clear ();
out =
pagecontext.pushbody ();
Sample code:
OutputStream Os=response.getoutputstream ();
Os.write (New String ("True
" + "nownum=" + nownum+ "= ="). GetBytes ());
Os.flush ();
Os.close ();

Out.clear ();
out = Pagecontext.pushbody ();
--------------------------------------------------------------------------------------------------------------- -------------------when JSP prints a picture to the page, using Response.getoutputstream () will have this hint:
Java.lang.IllegalStateException:getOutputStream ()
has already been called for this response,
Will throw the exception reason one: the JSP default output stream is PrintWriter, that is, <%%> something other than the default output way, If you try to use Servletoutputstream in your JSP, you can cause an error. Why not go directly to the servlet output (the Replication service method), delete anything except the%><% (including HTML tags, spaces, carriage returns, etc.) It should be. This should be the case for such a situation, delete all the content between the%><% including spaces and line breaks, and finally to eliminate spaces and line breaks, preferably plus a response.reset (). Reason two: In the Java EE API Reference has this: Servletresponse's Getwriter () method throws this exception,
Illegalstateexception-if the Getoutputstream method has already been called
For this response object and its Getoutputstream () method throws this exception. Illegalstateexception-if the Getoutputstream method has already been to this called object and both of the function declarations have this kind of EI Ther this and getoutputstream () May is called to write the body, not both. Either this and getwriter () May is called to write the body and not both. The above explanation also explains why you should use the following looping format when writing pictures to a page
OutputStream Output=response.getoutputstream ();
while ((Len=in.read (b)) >0)

{
output.write (B,0,len);

}
Output.flush ();
Instead of putting Response.getoutputstream (). write () into the loop body
Related Article

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.