How to Perform Exception Handling in JSP

In this tutorial how to perform Exception Handling in JSP is shown.
There are 3 ways to Handle Exception in JSP:

  1. Through isError page and Exception object
  2. Through Try and Catch block
  3. Through web.xml file

Code:

index.html

<html>
<head>
<title>Exception Handling</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”process.jsp” method=”POST”>
Value1: <input type=”text” name=”val1″><br>
Value2: <input type=”text” name=”val2″><br>
<input type=”submit” value=”Divide”>
</form>
</body>
</html>

1st method:

process.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page errorPage=”myerrorpage.jsp” %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Error Page</title>
</head>
<body>
<%
String val1=request.getParameter(“val1”);
String val2=request.getParameter(“val2”);
int a=Integer.parseInt(val1);
int b=Integer.parseInt(val2);
int ans=a/b;
out.println(“Answer= “+ans);
%>
</body>
</html>

myerrorpage.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page isErrorPage=”true”%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Error Page</title>
</head>
<body>
<h1>Sorry exception occurred</h1>
Exception is <%=exception%>
</body>
</html>

2nd method:

process.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Error Page</title>
</head>
<body>
<%
try {
String val1=request.getParameter(“val1”);
String val2=request.getParameter(“val2”);
int a=Integer.parseInt(val1);
int b=Integer.parseInt(val2);
int ans=a/b;
out.println(“Answer= “+ans);
}
catch(Exception e)
{
out.println(“Exception is: “+e);
}
%>
</body>
</html>

3rd method

process.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Error Page</title>
</head>
<body>
<%
String val1=request.getParameter(“val1”);
String val2=request.getParameter(“val2”);
int a=Integer.parseInt(val1);
int b=Integer.parseInt(val2);
int ans=a/b;
out.println(“Answer= “+ans);
%>
</body>
</html>

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”3.1″ xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<–1st method–>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/myerrorpage.jsp</location>
</error-page>
<–2nd method–>
<error-page>
<error-code>500</error-code>
<location>/myerrorpage.jsp</location>
</error-page>
</web-app>

Add a Comment

Your email address will not be published. Required fields are marked *