Every time I get a "405 method not allowed" response from the server, I want to redirect the user to a given url. However, I keep getting IllegalStateExceptions saying the response has already committed. Is there any way I can redirect the user without getting this exception?
I have the 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 the 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
开发者_JS百科Edit: Meant to add the stack trace:
09:01:33,326 ERROR [[MethodNotAllowedHandler]] Servlet.service() for servlet MethodNotAllowedHandler threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435) at com.ex.MethodNotAllowedHandler.doGet(MethodNotAllowedHandler.java:26) 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 org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:447) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292) at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:423) at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:342) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262) 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:446) at java.lang.Thread.run(Thread.java:619)
I got it to work, and I think I know why-- it seems this 405 exception is occurring more than once, so the 2nd time the handler catches it, the response is already committed. This fixed my 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 was already committed!");
}
return;
}
I'm not sure if this will work, can't you directly give the URL in the location attribute?
<error-page>
<error-code>405</error-code>
<location>http://www.google.com</location>
</error-page>
EDIT: Alright, I found that it won't work the above way. But the technique you have posted yourself worked for me. So what I suspect is that there are multiple redirects happening either through a filter or another JSP.
精彩评论