Java. Lang. illegalstateexception: cannot forward after response has been committed

Source: Internet
Author: User

Error message:

Severe: servlet. Service () for servlet showbaby threw exception
Java. Lang. illegalstateexception: cannot forward after response has been committed
At org. Apache. Catalina. Core. applicationdispatcher. doforward (applicationdispatcher. Java: 312)
At org. Apache. Catalina. Core. applicationdispatcher. Forward (applicationdispatcher. Java: 302)
At com. shyy. JB. epet. servlet. showbabyservlet. doget (showbabyservlet. Java: 37)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 690)
At javax. servlet. http. httpservlet. Service (httpservlet. Java: 803)
At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java: 290)
At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java: 206)
At com. shyy. JB. epet. Filter. setcharacterencodingfilter. dofilter (setcharacterencodingfilter. Java: 32)
At org. Apache. Catalina. Core. applicationfilterchain. internaldofilter (applicationfilterchain. Java: 235)
At org. Apache. Catalina. Core. applicationfilterchain. dofilter (applicationfilterchain. Java: 206)
At org. Apache. Catalina. Core. standardwrappervalve. Invoke (standardwrappervalve. Java: 233)
At org. Apache. Catalina. Core. standardcontextvalve. Invoke (standardcontextvalve. Java: 175)
At org. Apache. Catalina. Core. standardhostvalve. Invoke (standardhostvalve. Java: 128)
At org. Apache. Catalina. Valves. errorreportvalve. Invoke (errorreportvalve. Java: 102)
At org. Apache. Catalina. Core. standardenginevalve. Invoke (standardenginevalve. Java: 109)
At org. Apache. Catalina. connector. coyoteadapter. Service (coyoteadapter. Java: 286)
At org. Apache. Coyote. http11.http11processor. Process (http11processor. Java: 844)
At org. Apache. Coyote. http11.http11protocol $ http11connectionhandler. Process (http11protocol. Java: 583)
At org.apache.tomcat.util.net. jioendpoint $ worker. Run (jioendpoint. Java: 447)
At java. Lang. thread. Run (unknown source)

Source code:

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String id = request. getparameter ("ID ");
If (null = ID | ID. Equals ("")){
Request. setattribute ("error", "no pet number specified! ");
// Response. sendredirect ("/WEB-INF/JSP/error. jsp"); // redirection, changing the address bar address on the browser side
Request. getrequestdispatcher ("/WEB-INF/JSP/error. jsp"). Forward (request, response); // forward, jump on the server side, address unchanged
}
Integer petid = integer. valueof (ID );

Petinfobiz biz = new petinfobizimpl ();
Petinfo = biz. Load (petid); // load the pet information by ID
If (null = petinfo ){
Request. setattribute ("error", "no pet record specified! "); // A pet without this ID will jump to the error page
Request. getrequestdispatcher ("/WEB-INF/JSP/error. jsp"). Forward (request, response );
}
Request. setattribute ("item", petinfo );
Request. getrequestdispatcher ("/WEB-INF/JSP/PET/petinfo. jsp"). Forward (request, response );
}

This is a simple servlet. What I typed in the address bar is:

Http: // localhost: 8080/epet/showbaby. do? Id = 4

The database does not have the ID 4. In theory, the error page should be redirected, but no.

Error cause:

If {......} Else {......} This is a logical judgment statement. It only has if {} but does not have else {}. Sometimes it continues to execute external statements, causing errors.

Solution:

If (null = petinfo ){
Request. setattribute ("error", "no pet record specified! "); // A pet without this ID will jump to the error page
Request. getrequestdispatcher ("/WEB-INF/JSP/error. jsp"). Forward (request, response );
}Else {
Request. setattribute ("item", petinfo );
Request. getrequestdispatcher ("/WEB-INF/JSP/PET/petinfo. jsp"). Forward (request, response );

}
}

Add else {}

If (null = petinfo ){
Request. setattribute ("error", "no pet record specified! "); // A pet without this ID will jump to the error page
Request. getrequestdispatcher ("/WEB-INF/JSP/error. jsp"). Forward (request, response );

Return;
}
Request. setattribute ("item", petinfo );
Request. getrequestdispatcher ("/WEB-INF/JSP/PET/petinfo. jsp"). Forward (request, response );
}

Add a return after if, so that the statement outside if is not executed. OK. Solve the problem.

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.