You must not show the servler exception page to the client. You have to handle it.
You can design custom page to handle errors using page attribute. Like below,
The designed error page(errorPage.jsp):
<%@page isErrorPage=”true”%>
<html>
<body>
Hey there is an error occeared
<body>
<html>
The page that shows exception(myPage.jsp):
%@page errorPage=”errorPage.jsp”%
<html>
<body>
<%int x=10/0;% >
<body>
<html>
If any error occears then the container forewords to the error page.
ANOTHER WAY:
You can declare the error pages in the DD for the entire web app, and you can even configure different error pages for different exception types or HTTP error code type(404,500 etc).
The container uses <error-page> configuration in the DD as the default, but if a jsp has an explicit errorPage in the page directive then the container uses the directive.
Configuring error page in DD:
Declaring a catch all error-page:
This applies to everything in your web app. You can override it in all individual
jsps by adding errorPage in page directive.
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorPage.jsp</location>
</error-page>
Declaring error page for specific exception:
<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/arithmeticError.jsp</location>
</error-page>
Here if you have both declarations (Throwable and arithmeticexception) then all exceptions other than Arithmetic goes to the errorPage.jsp
Declaring error page base on status code:
<error-page>
<error-code>404</error-code>
<location>/notfounderror.jsp</location>
</error-page>
WE CAN ALSO USE JSTL <c:catch>:
The <c:catch> served as both try and catch. There is no separate try tag. You wrap the risky codes in the body of <c:catch> and the exception is caught right there. So after this tag the statements will be executed.
<html><body>
<c:catch>
<%int x=10/0;%>
<c:catch>
Hi I will be printed
</body></html>
Here we caught the exception instead of triggring to the error page.
But this is not an official error page. because there no no isErrorPage attribute in the page directive.
We can use var to get the exception object.
<html><body>
<c:catch var=”myException”>
<%int x=10/0;%>
<c:catch>
Hi I will be printed
Exception is: ${myException.message}
</body></html>
Here a new page scoped attribute named myException is created and assigned the
exception object. Now the myException is Throwable and a message property (as Throwable has a getMessage() method).
No comments:
Post a Comment