添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

// Tutorial //

Servlet Exception and Error Handling Example Tutorial

Published on August 3, 2022
Default avatar

By Pankaj

Servlet Exception and Error Handling Example Tutorial

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will look into Servlet Exception and Error Handling. Sometime back I wrote a post about Exception Handling in Java but when it comes to web application, we need more than normal exception handling in java.

Servlet Exception

Servlet Exception, javax.servlet.ServletException Since browser understand only HTML, when our application throw exception, servlet container processes the exception and generate a HTML response. This logic is specific to servlet container. I am using tomcat and getting this error page. If you will use some other servers like JBoss or Glassfish, you might get different error HTML response. The problem with this response is that it’s of no value to user. Also it’s showing our application classes and server details to user that makes no sense to user and it’s not good from security point of view.

Servlet Error

Servlet Error Again it’s a generic HTML generated by server on our application behalf and hold little to no value to the user.

Servlet Exception and Error Handling

Servlet Exception, Servlet 500 Internal Server Error If we try to access an invalid URL that will result in 404 response, we will get response like below image. Servlet Error, Servlet Error Handling, servlet 404 Doesn’t it look good and helps user to easily understand what happened and provides them a way to go to the correct location. It also avoids sending application sensitive information to the user. We should always have exception handlers in place for our web application. If you want to handle runtime exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable.

<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/AppExceptionHandler</location>
</error-page>

If there are multiple error-page entries, let’s say one for Throwable and one for IOException and application throws FileNotFoundException then it will be handled by error handler of IOException. You can also use JSP page as exception handler, just provide the location of jsp file rather than servlet mapping. That’s all for servlet exception handling in web application, I hope you liked it.

Download Servlet Exception Handling Example Project

Check out other articles in this series:

  • Java Web Application
  • Java Servlet Tutorial
  • Session Management in Java
  • Servlet Filter
  • Servlet Listeners
  • Cookies in Servlets
  • Servlet File Upload and Download Example
  • Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Pankaj

    author

    Still looking for an answer?

    Ask a question Search for more help

    Was this helpful?
    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 2, 2020

    It works really nice. Thank you so much for your amazing description.

    - Hamid Fazli

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 14, 2019

    Hello All, I know this maybe an old tutorial, but trying to get it to work with the current tomcat version is a problem. I get the filtering to work, but the issue with example is that tomcat is not display the display page. I know the code is working, since at the end of the last line of the code example I added a System.out.println message to display the status code. Any help in understanding why tomcat will not display the page ? Please advise, Clarence

    - Clarence Leslie

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    February 14, 2017

    Where do you put the AppExceptionHandler file???

    - bob white

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 2, 2017

    Good resources for web development. Thanks mate.

    - Vedant

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 9, 2014

    I just want to ask for help on how to trap the same data entered in the database using java servlet code…tnx in advance and merry christmas!!!

    - Jethruel Bustamante

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 3, 2014

    I very thankful to your website .this is very much helpful to me .and i leran ervery thing in java easily

    - Anjanee kumar

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 8, 2014

    I am trying to integrate hibernate with jsp servlet in a web application. The servlet does not show any error, but it generates a BLANK PAGE in the browser. When I removed a single line of code (whose purpose was, to call a method to insert data into sql table) then it was showing output. Please Help. No amount of Google-ing could solve it.

    - Madhavi

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 4, 2014

    It would be better to override service() rather than doGet() and doPost(), so it handles all HTTP methods and not just GET/POST. As-is, if there is an error for other methods it will result in a Method Not Allowed error from the error page, which on most servlet contains will result in a blank page or a nasty error page.

    - James Livingston

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 12, 2014

    Very Useful to learn servlet Exception .

    - JOTHIBASU