Error Handling in JSP
- Basically what kind of errors can expect in jsp
page?
- We know that jsp is nothing but a html with java
so tipically you would get some java type errors and some html type
errors in jsp page
- Lets we talk about java type errors:
Errors in java code in scriptlets:
- here every one writes certain java logic or
java code in jsp page in terms of scriptlets , expressions ,
declarations and all those stuff. But most of the time if at all
writing any kind of business logic in your jsp page will be writing
in a scriptlet.
- Scriptlet is the place where most of your java
programming errors in a jsp page would reside and how do you handle
these errors? We can write your basic system print statements to
track your different variables modifying all these but if at all
having your huge amount of code in your jsp page , huge amount of
java code in your jsp page and not able to figure of the issue, then
it is highly recommended to write the code in side the try ..catch
block and the even you will sup-rise to figure out the defects with
minutes when you use the try/catch block in jsp page.
- And most of these errors occur at run time so
basically when you are testing you probably would not come across
certain scenarios which result in these errors and suddenly out of
the blue they pop out the no where .
- So these are typically difficult to identify but
there is one type of error which is easy to identify and those are
the html type errors
Errors in Html
- we know that we got term of html scripting
elements in html, we have different element html <body>,
<table> and all those things.
- So you might by mistake write element name
incorrectly, or forget to close some element or something like that
so those kind of small html issues could occur in jsp page. And you
will find out a html error as soon as you the moment you launch your
page so if at all any html errors in the jsp page when you launch
your page , it is not going to show up properly and that is the
problem where we can immediately identify that there was some error
in html and you can fix that error.
- So these are very easy to figure out and they
can be caught during testing
Ans:
We know that a JSP is nothing but a html and this is what the end users sees right, so that means the end user entire world the jsp page is the face of your web application. So the end user really does not care what kind of complex algorithms you are using in the in the back. He does not really be care what kind of logic is used in the back end, the only thing the end user is worried about is what he sees on the page? whatever there on the page, end user likes it, whatever is there on the screen, the end user does not like it? Whatever business logic is write on the back-end is of no use goes in vain, so that is the reason why error handling in JSP pages extremely important.
…......................................................---------------------------------------
Lets just have a look at two different error pages which are shown and lets see which one is better than that one?
1. Error page 1.
- Error page 1:
How do you achieve it to display the error message 1?
- Error pages comes to the rescue
<html>
<head>
<title>My Error Page</title>
<head>
<body>
We are sorry, the page you are trying to access is currently not available .
Please try after some time .
</body>
</html>
- error pages are
nothing but simple jsp pages but small difference
- And the only
difference is you have this page directive attribute(ie.
IsErrorPage and you said that this attribute is “true”
- So as soon as
iserrorPage attribute is “true” the page directive
jsp page become some error page now. And it can be used an error
page.
- This error page
which would be showing to the end client
- So we know that to
show that some fancy error page to the client whenever there is an
error and we also developed error page now
- But how do you
display this error page to the end user? , the end user actually
requested for some other page we did not request for this page, so
how do you actually submit this page to the user?
We can do it in two ways:
- specify your error
page in your main page
- specify your error
page in web.xml
1. Specify your error page in your main page:
<%@page errorPage=”errPage.jsp”%>
<html>
<head>
<title>This is the main page</title>
</head>
<body>
….......................
…..........................
…...........................
//ERROR CODE GOES HERE
….............................
…...........................
</body>
</html>
- So whenever an
error is in my jsp page , I want to redirect it into error page
which I just coded now and show that beautiful message to the end
user. How do I do that ? I do that errorPage
attribute on the page directive.
- So whenever
there is a error in this jsp page the jsp engine is going to look at
the page directive and see if there is any error page
defined(errorPage=”errPage.jsp”). And if there is any error page
is defined, then it is going to redirect to the this error page ex:
errPage.jsp.
- So whenever
there is an error jsp page I'm redirected to this error page and
that error page is going to the end user which is a brief
description of the error message to the end user.
2. specify error page In web.xml
- Now lets do that
based on error I want to redirect the user to different error pages
so if the page is not found I will display some error message to the
user and there is some kind of null pointer exception or something
like that I will display some other page to the end user . How can
you do that you can do that, using the web.xml
…................
…................
<error-page>
<exception-type>com.a.myExp</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code> 404</error-code>
<location>/errorServlet</location>
</error-page>
….......................
…......................
</web-app>
- So you can
define error page even in web.xml
- So within the
<error- page > element , we write an <exception-type>
and say that whenever there is an exception of type
myExp in
one the jsp page I want to redirect it to the this error page I.e
error.jsp
- And
we also alternative say whenver there is an error code 404, I want
to redirect it this error page (I.e error.jsp)
- Here again we
would have noticed that two ways I can define this, 1. using
<exception-type> 2. using the <error-code>
2. error page number 2:
t simply displayes the 404 error message, error message which does not give any information so you do not really know this page available at all with this website down or something is wrong with the code or . So really dont have any idea of is happening behind this in this scenario 2 .
but in Scenario 1 system has displayed clearly neat message saying the reason for the error come back .
- So end user
point of view error page 1 is far more better than the error page 2
so you should give a user brief description of an error message of
why that error has happened or ask them to come back of try after
some time or something like that instead of just throwing 404 error
page can not be displayed error message .
<%@page isErrorPage=”true” %>
<html>
<head>
<title>My Error Page</title>
<head>
<body>
We are sorry, the page you are trying to access is currently not available .
Please try after some time .
</body>
</html>
No comments:
Post a Comment