java - How to avoid illegal state exception? -
every time "405 method not allowed" response server, want redirect user given url. however, keep getting illegalstateexceptions saying response has committed. there way can redirect user without getting exception?
i have following servlet:
public class methodnotallowedhandler extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { resp.sendredirect("http://www.google.com"); } }
and following entry in web.xml:
<servlet> <servlet-name>methodnotallowedhandler</servlet-name> <servlet-class>com.ex.methodnotallowedhandler</servlet-class> </servlet> <servlet-mapping> <servlet-name>methodnotallowedhandler</servlet-name> <url-pattern>/methodnotallowedhandler</url-pattern> </servlet-mapping> <error-page> <error-code>405</error-code> <location>/methodnotallowedhandler</location> </error-page>
thanks
edit: meant add stack trace:
09:01:33,326 error [[methodnotallowedhandler]] servlet.service() servlet methodnotallowedhandler threw exception java.lang.illegalstateexception @ org.apache.catalina.connector.responsefacade.sendredirect(responsefacade.java:435) @ com.ex.methodnotallowedhandler.doget(methodnotallowedhandler.java:26) @ javax.servlet.http.httpservlet.service(httpservlet.java:690) @ javax.servlet.http.httpservlet.service(httpservlet.java:803) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:290) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:654) @ org.apache.catalina.core.applicationdispatcher.processrequest(applicationdispatcher.java:447) @ org.apache.catalina.core.applicationdispatcher.doforward(applicationdispatcher.java:379) @ org.apache.catalina.core.applicationdispatcher.forward(applicationdispatcher.java:292) @ org.apache.catalina.core.standardhostvalve.custom(standardhostvalve.java:423) @ org.apache.catalina.core.standardhostvalve.status(standardhostvalve.java:342) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:143) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) @ org.jboss.web.tomcat.service.jca.cachedconnectionvalve.invoke(cachedconnectionvalve.java:157) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:262) @ org.apache.coyote.http11.http11processor.process(http11processor.java:844) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:583) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:446) @ java.lang.thread.run(thread.java:619)
i got work, , think know why-- seems 405 exception occurring more once, 2nd time handler catches it, response committed. fixed problem:
@override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { if (!resp.iscommitted()) { resp.sendredirect("http://www.google.com"); } else { log.info(" response committed!"); } return; }
Comments
Post a Comment