Jsp- Error Handling in jsp
Introduction:
There are two types of error that occur during lifecycle of a jsp.
1). Translation time errors
2). Request time errors
- Translation time errors: occurs during the initial request
- Request time errors- occurs during the subsequent request
Translation Time Errors:
These errors occur when the jsp page is requested first and the jsp source file is translated into the servlet file. These errors are compilation failure errors. These types of error are reported to the client with error status code 500 or server Error. The all syntax error are called as Translation Time Errors.
TranslationError.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP Error Page < /title >
< /head >
< body >
< h1 > Hello World! < /h1 >
< %
out.println("Welcome to JSP");
% >
< /body >
< /html >
jsp- request time error
This type of error occurs during the request time. In request time error, the jsp source file is translated into the servlet file. But it will not produce any result. It will show error. This is known as run time error.
The exception that are not handled will be forwarded to the error page as specified in the jsp < @ page errorPage=”jsp” >
Step 1:
Creating Error Page
We have to create jsp and inform the jsp engine that the page is jsp error page.
How to inform to the jsp engine?
We include isErrorPage=”true” in < @ page isErrorPage=”true” > tag
ErrorPage.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< %@page isErrorPage="true" import="java.io.*" % >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP Error Page < /title >
< /head >
< body >
< %=
exception.getMessage()
% >
< /body >
< /html >
ErrorJsp.jsp
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< %@page errorPage="ErrorPage.jsp" % >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head >
< title > JSP throw uncaught Exception < /title >
< /head >
< body >
< %!
int a=5,b=0,c;
% >
< %
c=a/b;
out.println(c);
% >
< /body >
< /html >
No comments:
Post a Comment