How exceptions can be handled in JSP scripts? Explain with suitable JSP script.

 EXCEPTION HANDLING IN JSP

An exception is normally an object that is thrown at runtime. Exception Handling is the process to handle runtime errors. There may occur exceptions at any time in our web application. So handling exceptions is a safer side for the web developer. YR gives us an option to specify the Error Page for each JSP. Whenever the page throws an exception, the P container automatically invokes the error page. As we know, the exception is an implicit object of type Throwable class. This object can be used to print the exception. But it can only be used in error pages. To set up an error page, use the <%@ page errorPagexxxp' %> directive.

Example

//index.jsp File <form action="process.jsp">

<table>

<tr>

<td>First Number</td>

<td><input type="text" name="n1" /></td>

</tr>

<tr>

<td>Second Number</td>

<td><input type="text" name="n2" /> </td>

</tr>

<tr>

<td colspan=2 align=center>

<input type="submit" value="divide"/>

</td>

</tr>

</form>

//process.jsp File

page errorPage="error.jsp" %>

String num1=request.getParameter("n1"); 

String num2=request.getParameter("n2");

int a=Integer.parseInt(num1);

int b=Integer.parseInt(num2);

int c=a/b;

out.print("division of numbers is: "+c);

%>

//error.jsp

<% page isError Page="true" %>

<h3>Sorry an exception occured!</h3>

 Exception is: <%= exception.getMessage() %>


Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC