{"id":16686,"date":"2013-08-27T16:00:55","date_gmt":"2013-08-27T13:00:55","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=16686"},"modified":"2013-08-27T08:15:58","modified_gmt":"2013-08-27T05:15:58","slug":"servlet-exception-and-error-handling-example-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html","title":{"rendered":"Servlet Exception and Error Handling Example Tutorial"},"content":{"rendered":"<p>Sometime back I wrote a post about <strong><a href=\"http:\/\/www.journaldev.com\/1696\/java-exception-handling-tutorial-with-examples-and-best-practices\">Exception Handling in Java<\/a><\/strong> but when it comes to web application, we need more than normal exception handling in java.<\/p>\n<h2>Servlet Exception<\/h2>\n<p>If you notice, doGet() and doPost() methods throw <code>ServletException<\/code> and <code>IOException<\/code>, let\u2019s see what happens when we throw these exception from our application. I will write a simple servlet that will throw the ServletException.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n<strong><code>MyExceptionServlet.java<\/code><\/strong><\/p>\n<pre class=\" brush:java\">package com.journaldev.servlet.exception;\r\n\r\nimport java.io.IOException;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\n@WebServlet(\"\/MyExceptionServlet\")\r\npublic class MyExceptionServlet extends HttpServlet {\r\n\tprivate static final long serialVersionUID = 1L;\r\n\r\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\t\tthrow new ServletException(\"GET method is not supported.\");\r\n\t}\r\n\r\n}<\/pre>\n<p>Now when we invoke this servlet through browser with GET method, we get response like below image.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-User.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-16781\" alt=\"Servlet-Exception-User\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-User-300x135.png\" width=\"300\" height=\"135\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-User-300x135.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-User.png 790w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>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 but if you will use some other servers like JBoss or Glassfish, you might get different error HTML response.<\/p>\n<p>The problem with this response is that it\u2019s of no value to user. Also it\u2019s showing our application classes and server details to user that makes no sense to user and it\u2019s not good from security point of view.<\/p>\n<h2>Servlet Error<\/h2>\n<p>I am sure you must have seen 404 error when you are trying to hit a URL that doesn\u2019t exists. Let\u2019s see how our servlet container responds to 404 error. If we send request for an invalid URL, we get response HTML like below image.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-404-Error.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-16782\" alt=\"Servlet-404-Error\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-404-Error-300x103.png\" width=\"300\" height=\"103\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-404-Error-300x103.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-404-Error.png 666w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Again it\u2019s a generic HTML generated by server on our behalf and hold little to no value to the user.<\/p>\n<h2>Servlet Exception and Error Handling<\/h2>\n<p>Servlet API provides support for custom Exception and Error Handler servlets that we can configure in deployment descriptor, the whole purpose of these servlets are to handle the Exception or Error raised by application and send HTML response that is useful for the user. We can provide link to application home page or some details to let user know what went wrong.<\/p>\n<p>So first of all we need to create a custom Exception and Error Handler servlet. We can have multiple exception and error handler servlets for the application but for simplicity I will create a single servlet and use it for both exceptions and errors.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><strong><code>AppExceptionHandler.java<\/strong><\/code><\/p>\n<pre class=\" brush:java\">package com.journaldev.servlet.exception;\r\n\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\n\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\n@WebServlet(\"\/AppExceptionHandler\")\r\npublic class AppExceptionHandler extends HttpServlet {\r\n\tprivate static final long serialVersionUID = 1L;\r\n\r\n\tprotected void doGet(HttpServletRequest request,\r\n\t\t\tHttpServletResponse response) throws ServletException, IOException {\r\n\t\tprocessError(request, response);\r\n\t}\r\n\r\n\tprotected void doPost(HttpServletRequest request,\r\n\t\t\tHttpServletResponse response) throws ServletException, IOException {\r\n\t\tprocessError(request, response);\r\n\t}\r\n\r\n\tprivate void processError(HttpServletRequest request,\r\n\t\t\tHttpServletResponse response) throws IOException {\r\n\t\t\/\/ Analyze the servlet exception\r\n\t\tThrowable throwable = (Throwable) request\r\n\t\t\t\t.getAttribute(\"javax.servlet.error.exception\");\r\n\t\tInteger statusCode = (Integer) request\r\n\t\t\t\t.getAttribute(\"javax.servlet.error.status_code\");\r\n\t\tString servletName = (String) request\r\n\t\t\t\t.getAttribute(\"javax.servlet.error.servlet_name\");\r\n\t\tif (servletName == null) {\r\n\t\t\tservletName = \"Unknown\";\r\n\t\t}\r\n\t\tString requestUri = (String) request\r\n\t\t\t\t.getAttribute(\"javax.servlet.error.request_uri\");\r\n\t\tif (requestUri == null) {\r\n\t\t\trequestUri = \"Unknown\";\r\n\t\t}\r\n\r\n\t\t\/\/ Set response content type\r\n\t      response.setContentType(\"text\/html\");\r\n\r\n\t      PrintWriter out = response.getWriter();\r\n\t      out.write(\"&lt;html&gt;&lt;head&gt;&lt;title&gt;Exception\/Error Details&lt;\/title&gt;&lt;\/head&gt;&lt;body&gt;\");\r\n\t      if(statusCode != 500){\r\n\t    \t  out.write(\"&lt;h3&gt;Error Details&lt;\/h3&gt;\");\r\n\t    \t  out.write(\"&lt;strong&gt;Status Code&lt;\/strong&gt;:\"+statusCode+\"&lt;br&gt;\");\r\n\t    \t  out.write(\"&lt;strong&gt;Requested URI&lt;\/strong&gt;:\"+requestUri);\r\n\t      }else{\r\n\t    \t  out.write(\"&lt;h3&gt;Exception Details&lt;\/h3&gt;\");\r\n\t    \t  out.write(\"&lt;ul&gt;&lt;li&gt;Servlet Name:\"+servletName+\"&lt;\/li&gt;\");\r\n\t    \t  out.write(\"&lt;li&gt;Exception Name:\"+throwable.getClass().getName()+\"&lt;\/li&gt;\");\r\n\t    \t  out.write(\"&lt;li&gt;Requested URI:\"+requestUri+\"&lt;\/li&gt;\");\r\n\t    \t  out.write(\"&lt;li&gt;Exception Message:\"+throwable.getMessage()+\"&lt;\/li&gt;\");\r\n\t    \t  out.write(\"&lt;\/ul&gt;\");\r\n\t      }\r\n\r\n\t      out.write(\"&lt;br&gt;&lt;br&gt;\");\r\n\t      out.write(\"&lt;a href=\\\"index.html\\\"&gt;Home Page&lt;\/a&gt;\");\r\n\t      out.write(\"&lt;\/body&gt;&lt;\/html&gt;\");\r\n\t}\r\n}<\/pre>\n<p>Let\u2019s see how we can configure it in deployment descriptor and then we will understand it\u2019s implementation and how it works.<\/p>\n<p><strong><code>web.xml<\/strong><\/code><\/p>\n<pre class=\" brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\" xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_3_0.xsd\" version=\"3.0\"&gt;\r\n  &lt;display-name&gt;ServletExceptionHandling&lt;\/display-name&gt;\r\n  &lt;welcome-file-list&gt;\r\n    &lt;welcome-file&gt;index.html&lt;\/welcome-file&gt;\r\n  &lt;\/welcome-file-list&gt;\r\n\r\n  &lt;error-page&gt;\r\n  \t&lt;error-code&gt;404&lt;\/error-code&gt;\r\n  \t&lt;location&gt;\/AppExceptionHandler&lt;\/location&gt;\r\n  &lt;\/error-page&gt;\r\n\r\n  &lt;error-page&gt;\r\n  &lt;exception-type&gt;javax.servlet.ServletException&lt;\/exception-type&gt;\r\n  &lt;location&gt;\/AppExceptionHandler&lt;\/location&gt;\r\n  &lt;\/error-page&gt;\r\n&lt;\/web-app&gt;<\/pre>\n<p>As you can see, it\u2019s very easy to specify Exception handler servlets for the application using <strong>error-page<\/strong> element. Each error-page element should have either <strong>error-code<\/strong> or <strong>exception-type<\/strong> element. We define the exception handler servlet in <strong>location<\/strong> element.<\/p>\n<p>Based on above configuration, if the application throw 404 error or ServletException, it will be handled by AppExceptionHandler servlet.<\/p>\n<p>When such exception and error scenario appears, servlet container will invoke the corresponding HTTP method of the Exception Handler servlet and pass the request and response object. Notice that I have provided implementation of both doGet() and doPost() methods so that it can handle GET and POST requests and using a common method to process them.<\/p>\n<p>Before servlet container invokes the servlet to handle the exception, it sets some attributes in the request to get useful information about the exception, some of them are <strong>javax.servlet.error.exception<\/strong>, <strong>javax.servlet.error.status_code<\/strong>, <strong>javax.servlet.error.servlet_name<\/strong> and <strong>javax.servlet.error.request_uri<\/strong>.<\/p>\n<p>For exception, status code is always 500 that corresponds to the \u201cInternal Server Error\u201d, for other types of error we get different error codes such as 404, 403 etc.<\/p>\n<p>Using the status code, our implementation presents different types of HTML response to the user. It also provides a hyperlink to the home page of the application.<\/p>\n<p>Now when we will hit our servlet that is throwing ServletException, we will get a response like below image.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-Handling-500-Code.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-16783\" alt=\"Servlet-Exception-Handling-500-Code\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-Handling-500-Code-300x158.png\" width=\"300\" height=\"158\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-Handling-500-Code-300x158.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Exception-Handling-500-Code.png 524w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If we try to access an invalid URL that will result in 404 response, we will get response like below image.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Error-Handling-404.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-16784\" alt=\"Servlet-Error-Handling-404\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Error-Handling-404-300x129.png\" width=\"300\" height=\"129\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Error-Handling-404-300x129.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/08\/Servlet-Error-Handling-404.png 444w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Doesn\u2019t 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.<\/p>\n<p>If you want to handle runtime exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable.<\/p>\n<pre class=\" brush:xml\">&lt;error-page&gt;\r\n  &lt;exception-type&gt;java.lang.Throwable&lt;\/exception-type&gt;\r\n  &lt;location&gt;\/AppExceptionHandler&lt;\/location&gt;\r\n&lt;\/error-page&gt;<\/pre>\n<p>If there are multiple error-page entries, let\u2019s say one for Throwable and one for IOException and application throws FileNotFoundException then it will be handled by error handler of IOException.<\/p>\n<p>You can also use JSP page as exception handler, just provide the location of jsp file rather than servlet mapping.<\/p>\n<p>That\u2019s all for exception handling in web application, I hope you liked it.<\/p>\n<ul>\n<li><strong><a href=\"http:\/\/cdn1.journaldev.com\/wp-content\/uploads\/servlet\/ServletExceptionHandling.zip\" target=\"_blank\">Download ServletExceptionHandling Project<\/a><\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/www.journaldev.com\/1973\/servlet-exception-and-error-handling-example-tutorial\">Servlet Exception and Error Handling Example Tutorial<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Pankaj Kumar at the <a href=\"http:\/\/www.journaldev.com\/\">Developer Recipes<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 If you notice, doGet() and doPost() methods throw ServletException and IOException, let\u2019s see what happens when we throw these exception from our application. I will write &hellip;<\/p>\n","protected":false},"author":26,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[845],"class_list":["post-16686","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-servlet"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Servlet Exception and Error Handling Example Tutorial<\/title>\n<meta name=\"description\" content=\"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.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Servlet Exception and Error Handling Example Tutorial\" \/>\n<meta property=\"og:description\" content=\"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.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/JournalDev\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-27T13:00:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Pankaj Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/journaldev\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Pankaj Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html\"},\"author\":{\"name\":\"Pankaj Kumar\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/4e0e33e2f8fd3dcdb0b5239f79fdddc0\"},\"headline\":\"Servlet Exception and Error Handling Example Tutorial\",\"datePublished\":\"2013-08-27T13:00:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html\"},\"wordCount\":796,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Servlet\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html\",\"name\":\"Servlet Exception and Error Handling Example Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2013-08-27T13:00:55+00:00\",\"description\":\"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.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/08\\\/servlet-exception-and-error-handling-example-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Servlet Exception and Error Handling Example Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/4e0e33e2f8fd3dcdb0b5239f79fdddc0\",\"name\":\"Pankaj Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g\",\"caption\":\"Pankaj Kumar\"},\"sameAs\":[\"http:\\\/\\\/www.journaldev.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/JournalDev\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/journaldev\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Pankaj-Kumar\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Servlet Exception and Error Handling Example Tutorial","description":"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.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Servlet Exception and Error Handling Example Tutorial","og_description":"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.","og_url":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/JournalDev","article_published_time":"2013-08-27T13:00:55+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Pankaj Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/journaldev","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Pankaj Kumar","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html"},"author":{"name":"Pankaj Kumar","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/4e0e33e2f8fd3dcdb0b5239f79fdddc0"},"headline":"Servlet Exception and Error Handling Example Tutorial","datePublished":"2013-08-27T13:00:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html"},"wordCount":796,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Servlet"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html","name":"Servlet Exception and Error Handling Example Tutorial","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2013-08-27T13:00:55+00:00","description":"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.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/08\/servlet-exception-and-error-handling-example-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Servlet Exception and Error Handling Example Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/4e0e33e2f8fd3dcdb0b5239f79fdddc0","name":"Pankaj Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2f858dbb96781814cab524da3bfeeecf59a9327a985d0d8d35f20ba80b964586?s=96&d=mm&r=g","caption":"Pankaj Kumar"},"sameAs":["http:\/\/www.journaldev.com\/","https:\/\/www.facebook.com\/JournalDev","https:\/\/x.com\/https:\/\/twitter.com\/journaldev"],"url":"https:\/\/www.javacodegeeks.com\/author\/Pankaj-Kumar"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=16686"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16686\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=16686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=16686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=16686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}