{"id":2612,"date":"2017-09-02T07:29:56","date_gmt":"2017-09-02T07:29:56","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2612"},"modified":"2017-09-02T07:30:18","modified_gmt":"2017-09-02T07:30:18","slug":"how-to-perform-exception-handling-in-jsp","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-perform-exception-handling-in-jsp\/","title":{"rendered":"How to Perform Exception Handling in JSP"},"content":{"rendered":"<p>In this tutorial how to perform Exception Handling in JSP is shown.<br \/>\nThere are 3 ways to Handle Exception in JSP:<\/p>\n<ol>\n<li>Through isError page and Exception object<\/li>\n<li>Through Try and Catch block<\/li>\n<li>Through web.xml file<\/li>\n<\/ol>\n<p>Code:<\/p>\n<p>index.html<\/p>\n<p>&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Exception Handling&lt;\/title&gt;<br \/>\n&lt;meta charset=&#8221;UTF-8&#8243;&gt;<br \/>\n&lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;form action=&#8221;process.jsp&#8221; method=&#8221;POST&#8221;&gt;<br \/>\nValue1: &lt;input type=&#8221;text&#8221; name=&#8221;val1&#8243;&gt;&lt;br&gt;<br \/>\nValue2: &lt;input type=&#8221;text&#8221; name=&#8221;val2&#8243;&gt;&lt;br&gt;<br \/>\n&lt;input type=&#8221;submit&#8221; value=&#8221;Divide&#8221;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>1st method:<\/p>\n<p>process.jsp<\/p>\n<p>&lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt;<br \/>\n&lt;%@page errorPage=&#8221;myerrorpage.jsp&#8221; %&gt;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt;<br \/>\n&lt;title&gt;Error Page&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;%<br \/>\nString val1=request.getParameter(&#8220;val1&#8221;);<br \/>\nString val2=request.getParameter(&#8220;val2&#8221;);<br \/>\nint a=Integer.parseInt(val1);<br \/>\nint b=Integer.parseInt(val2);<br \/>\nint ans=a\/b;<br \/>\nout.println(&#8220;Answer= &#8220;+ans);<br \/>\n%&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>myerrorpage.jsp<\/p>\n<p>&lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt;<br \/>\n&lt;%@page isErrorPage=&#8221;true&#8221;%&gt;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt;<br \/>\n&lt;title&gt;Error Page&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;h1&gt;Sorry exception occurred&lt;\/h1&gt;<br \/>\nException is &lt;%=exception%&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>2nd method:<\/p>\n<p>process.jsp<\/p>\n<p>&lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt;<br \/>\n&lt;title&gt;Error Page&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;%<br \/>\ntry {<br \/>\nString val1=request.getParameter(&#8220;val1&#8221;);<br \/>\nString val2=request.getParameter(&#8220;val2&#8221;);<br \/>\nint a=Integer.parseInt(val1);<br \/>\nint b=Integer.parseInt(val2);<br \/>\nint ans=a\/b;<br \/>\nout.println(&#8220;Answer= &#8220;+ans);<br \/>\n}<br \/>\ncatch(Exception e)<br \/>\n{<br \/>\nout.println(&#8220;Exception is: &#8220;+e);<br \/>\n}<br \/>\n%&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>3rd method<\/p>\n<p>process.jsp<\/p>\n<p>&lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt;<br \/>\n&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt;<br \/>\n&lt;title&gt;Error Page&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;%<br \/>\nString val1=request.getParameter(&#8220;val1&#8221;);<br \/>\nString val2=request.getParameter(&#8220;val2&#8221;);<br \/>\nint a=Integer.parseInt(val1);<br \/>\nint b=Integer.parseInt(val2);<br \/>\nint ans=a\/b;<br \/>\nout.println(&#8220;Answer= &#8220;+ans);<br \/>\n%&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>web.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br \/>\n&lt;web-app version=&#8221;3.1&#8243; xmlns=&#8221;http:\/\/xmlns.jcp.org\/xml\/ns\/javaee&#8221; xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:schemaLocation=&#8221;http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd&#8221;&gt;<br \/>\n&lt;session-config&gt;<br \/>\n&lt;session-timeout&gt;<br \/>\n30<br \/>\n&lt;\/session-timeout&gt;<br \/>\n&lt;\/session-config&gt;<br \/>\n&lt;&#8211;1st method&#8211;&gt;<br \/>\n&lt;error-page&gt;<br \/>\n&lt;exception-type&gt;java.lang.Exception&lt;\/exception-type&gt;<br \/>\n&lt;location&gt;\/myerrorpage.jsp&lt;\/location&gt;<br \/>\n&lt;\/error-page&gt;<br \/>\n&lt;&#8211;2nd method&#8211;&gt;<br \/>\n&lt;error-page&gt;<br \/>\n&lt;error-code&gt;500&lt;\/error-code&gt;<br \/>\n&lt;location&gt;\/myerrorpage.jsp&lt;\/location&gt;<br \/>\n&lt;\/error-page&gt;<br \/>\n&lt;\/web-app&gt;<\/p>\n<p><a href=\"https:\/\/youtu.be\/4Sl9R9K0uoE\">https:\/\/youtu.be\/4Sl9R9K0uoE<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to perform Exception Handling in JSP is shown. There are 3 ways to Handle Exception in JSP: Through isError page and Exception object Through Try and Catch block Through web.xml file Code: index.html &lt;html&gt; &lt;head&gt; &lt;title&gt;Exception Handling&lt;\/title&gt; &lt;meta charset=&#8221;UTF-8&#8243;&gt; &lt;meta name=&#8221;viewport&#8221; content=&#8221;width=device-width, initial-scale=1.0&#8243;&gt; &lt;\/head&gt; &lt;body&gt; &lt;form action=&#8221;process.jsp&#8221; method=&#8221;POST&#8221;&gt; Value1: &lt;input type=&#8221;text&#8221; name=&#8221;val1&#8243;&gt;&lt;br&gt; Value2: &lt;input type=&#8221;text&#8221; name=&#8221;val2&#8243;&gt;&lt;br&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;Divide&#8221;&gt; &lt;\/form&gt; &lt;\/body&gt; &lt;\/html&gt; 1st method: process.jsp &lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt; &lt;%@page errorPage=&#8221;myerrorpage.jsp&#8221; %&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt; &lt;title&gt;Error Page&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; &lt;% String val1=request.getParameter(&#8220;val1&#8221;); String val2=request.getParameter(&#8220;val2&#8221;); int a=Integer.parseInt(val1); int b=Integer.parseInt(val2); int ans=a\/b; out.println(&#8220;Answer= &#8220;+ans); %&gt; &lt;\/body&gt; &lt;\/html&gt; myerrorpage.jsp &lt;%@page contentType=&#8221;text\/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt; &lt;%@page isErrorPage=&#8221;true&#8221;%&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=UTF-8&#8243;&gt; &lt;title&gt;Error Page&lt;\/title&gt; &lt;\/head&gt; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[125,150,151,124,145],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2612"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2612"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2612\/revisions"}],"predecessor-version":[{"id":2614,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2612\/revisions\/2614"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}