简体   繁体   中英

Handling servlets exceptions

I am getting troubles with handling exceptions in web servlet app. I've created ErrorHandler servlet, which handles all incoming exceptions and added entry in web.xml file :

<servlet>
    <servlet-name>ErrorHandler</servlet-name>
    <servlet-class>ServletPackage.ErrorHandler</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ErrorHandler</servlet-name>
    <url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/ErrorHandler</location>
</error-page>

The problem is the server does not invoke ErrorHandler servlet at all. Glassfish just throws Exception in the console and web browser refreshes the servlet which thrown exception (with no data due to unhandled exception).

Warning:   StandardWrapperValve[SessionChecker]: Servlet.service() for servlet SessionChecker threw exception
javax.servlet.ServletException: Forced exception thrown
    at ServletPackage.SessionChecker.processRequest(SessionChecker.java:158)
    at ServletPackage.SessionChecker.doPost(SessionChecker.java:174)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
   .
   .
   .

Any suggestions?

I had this same issue. All over the web are examples as you've shown and when I tried it it seemed that my handler servlet was just being ignored completely.

What worked for me to make sure that my error handling servlet implemented both doGet and doPost:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    processError(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    processError(request, response);
}

private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
    ...
}

The examples that I had based my servlet on had only implemented doGet. So while my servlet was being loaded and called on errors, nothing was happening with those errors. Implementing doPost to handle the errors the same way solved this for me.

If the exception is being thrown from your web server the exception handler you have declared in the web.xml is not ever going to deal with it.

The exception handler in the web.xml is to handle request coming into the web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM