{"id":1693,"date":"2016-09-22T04:12:37","date_gmt":"2016-09-22T04:12:37","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1693"},"modified":"2016-09-22T04:18:55","modified_gmt":"2016-09-22T04:18:55","slug":"exception-handling-in-jsp-pages","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/exception-handling-in-jsp-pages\/","title":{"rendered":"Exception handling in JSP Pages"},"content":{"rendered":"<p><strong>Exception Handling in JSP<\/strong><\/p>\n<ol>\n<li>By Using Error Page<\/li>\n<li>By Using PageContext<\/li>\n<li>By Using Try&#8230;Catch Block<\/li>\n<li>Specifying default error page in the web.xml <\/li>\n<\/ol>\n<p><strong>Using Error Page<\/strong><br \/>\nTo set up an error page, use the <%@ page errorPage=\"xxx\" %> directive<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ page errorPage=&quot;ShowError.jsp&quot; %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n   &lt;title&gt;Error Handling Example&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;%\r\n   \/\/ Throw an exception to invoke the error page\r\n   int x = 1;\r\n   if (x == 1)\r\n   {\r\n      throw new RuntimeException(&quot;Error condition!!!&quot;);\r\n   }\r\n%&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Error-handling page includes the directive <%@ page isErrorPage=\"true\" %>. This directive causes the JSP compiler to generate the exception instance variable.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ page isErrorPage=&quot;true&quot; %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Show Error Page&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;Opps...&lt;\/h1&gt;\r\n&lt;p&gt;Sorry, an error occurred.&lt;\/p&gt;\r\n&lt;p&gt;Here is the exception stack trace: &lt;\/p&gt;\r\n&lt;pre&gt;\r\n&lt;% exception.printStackTrace(response.getWriter()); %&gt;\r\n&lt;\/pre&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Using JSTL tags in PageContext<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http:\/\/java.sun.com\/jsp\/jstl\/core&quot; %&gt;\r\n&lt;%@page isErrorPage=&quot;true&quot; %&gt;\r\n&lt;table width=&quot;100%&quot; border=&quot;1&quot;&gt;\r\n&lt;tr valign=&quot;top&quot;&gt;\r\n&lt;td width=&quot;40%&quot;&gt;&lt;b&gt;Error:&lt;\/b&gt;&lt;\/td&gt;\r\n&lt;td&gt;${pageContext.exception}&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;tr valign=&quot;top&quot;&gt;\r\n&lt;td&gt;&lt;b&gt;URI:&lt;\/b&gt;&lt;\/td&gt;\r\n&lt;td&gt;${pageContext.errorData.requestURI}&lt;\/td&gt;\r\n&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n<\/pre>\n<p><strong>Using Try Catch Block:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n   &lt;title&gt;Try...Catch Example&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;%\r\n   try{\r\n      int i = 1;\r\n      i = i \/ 0;\r\n      out.println(&quot;The answer is &quot; + i);\r\n   }\r\n   catch (Exception e){\r\n      out.println(&quot;An exception occurred: &quot; + e.getMessage());\r\n   }\r\n%&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Specifying default error page in the web.xml<\/strong><br \/>\n<strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;error-page&gt;\r\n   &lt;error-code&gt;404&lt;\/error-code&gt;\r\n   &lt;location&gt;\/error404.html&lt;\/location&gt;\r\n&lt;\/error-page&gt;\r\n<\/pre>\n<p><strong>Sample.jsp<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ page errorPage=&quot;error.jsp&quot; %&gt;  \r\n&lt;%  \r\n   String s=null;\r\n   s.length();\r\n%&gt;\r\n<\/pre>\n<p><strong>error.jsp<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&lt;%@ page isErrorPage=&quot;true&quot; %&gt;\r\n&lt;%= exception.getMessage() %&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exception Handling in JSP By Using Error Page By Using PageContext By Using Try&#8230;Catch Block Specifying default error page in the web.xml Using Error Page To set up an error page, use the directive &lt;%@ page errorPage=&quot;ShowError.jsp&quot; %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Error Handling Example&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;% \/\/ Throw an exception to invoke the error page&hellip; <a href=\"https:\/\/codethataint.com\/blog\/exception-handling-in-jsp-pages\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-1693","post","type-post","status-publish","format-standard","hentry","category-jsp"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1693"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions"}],"predecessor-version":[{"id":1696,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1693\/revisions\/1696"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}