Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Create Error Page in J2EE

This tutorial will help the readers to learn on how to create error pages such as 401, 404 etc making use of J2EE.

There are two ways to define Error page in Java web application written using Servlet and JSP. First way is page wise error page which is defined on each jsp page and if there is any unhanded exception thrown from that page, corresponding error page will be displayed. Second approach is an application wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page specific error page defined.

In this tutorial we will see both approach to declare error page in JSP and when should we use page specific error page and when should we choose generate default application wide error page in Java web application.

Introduction to HTTP standard error codes

This section focus on the existing http errors we can face, each error type is associated with a server returned http error code :

  • class 1 return codes (1xx) : Information
This one is a new return code which is not 100% supported and normally and only provides information to the client about the request.
  • class 2 return codes (2xx) : Success response, the request has been correctly executed -> expected answer from server (http code 200).
  • class 3 return codes (3xx) : Redirection response. The resource has moved and is not any more at this URL.

Here are the errors we will be interested in.

  • class 4 return codes (4xx) : Error on client side. Probably most known of all is error 404 : Not Found
Such errors indicate something went wrong on the client side probably a mistake in the requested URL. This is not an issue you can avoid however displaying something attractive doesn’t cost that much.
  • class 5 return codes (5xx) : Error on client side. Something went wrong on the server in that case it can data access issue, bugs, etc… but it is developers responsibility and in that case displaying something which tells the user “his action has been cancelled because of a technical problem” is mandatory else the user can wait for while before he receives his confirmation…

Page Specific Error page in JSP

Every JSP page has an attribute called "errorpage" on page directive, by using this attribute you can define an error page for any particular JSP. After that if any unhandled Exception thrown from that JSP , this error page will be invoked. In order to make any JSP page as an error page you need to use "isErrorPage" attribute of page directive and mark it true.

Listing1: Defining the error.jsp page

//error.jsp
<%@ page isErrorPage="true"%>
//login.jsp
<%@ page errorPage="error.jsp"%>

This is preferred way of showing error messages in Java web application if you have custom error messages based on JSP and it also supersede any application wide error page defined in web.xml.

Error page in Java Web Application JSP Servlet

Application wide Error page in Java web application

There is another way to define error pages in java web application written using servlet and JSP. This is called application wide error page or default/general error page because it’s applicable to whole web application instead of any particular servlet or JSP. It is a recommended practice for every Java web application to have a default error page in addition of page specific error pages. This error page is defined in web.xml by using tag <error-page>. <error-page> allows you to define custom error message based upon HTTP error code or any Java Exception. You can define a default error message for all exception by specifying <exception-type> as java.lang.Throwable and it would be applicable to all exception thrown form any Servlet or JSP from web application.

Here is an example of declaring default error page in Java web application based on HTTP Errorcode and Java Exception type.

Listing2: Default Error page based on Exception type:A

<error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.htm</location>
</error-page>

Listing3: Default Error page based on HTTP Error code:

<error-page>
    <error-code>500</error-code>
    <location>/internal-server-error.htm</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/page-not-found-error.htm</location>
</error-page>

This is how we define custom error page in Java application both page specific and an application wide default error page. Important point to note is that page specific error pages takes precedence over application wide default.

Create your custom error pages

First of all you have to design the pages you want to display to user in case of error: let’s say 2 pages, 1 for 4xx errors and another for 5xx errors (however another for 3xx can be useful). You can use whatever you want (html, jsp, jsf, etc…) but be aware that if something went wrong on server side maybe your Tomcat server is having issues and therefore you shouldn’t be charging it with jsp pages or something else. That is why we always recommend building error pages using HTML.

Now we have:

  • 400.html -> telling the user did something wrong
  • 500.html -> telling the server did something wrong

Add it to your deployment descriptor (web.xml)

Now the only thing we have to do is to say our J2EE server we want to use these pages in case of errors. Since only integers are allowed for error-code we cannot use patterns, therefore we list all of them.

Listing4: Using the pages in case of errors

   <error-page>
		<error-code>400</error-code>
		<location>/404.html</location>
	</error-page>
        <error-page>
		<error-code>401</error-code>
		<location>/404.html</location>
	</error-page>
...
         <error-page>
		<error-code>500</error-code>
		<location>/500.html</location>
	</error-page>
        <error-page>
		<error-code>501</error-code>
		<location>/500.html</location>
	</error-page>

Conclusion

That’s all on the tutorial to create some nice and attractive error pages to your customers. Hope you had a nice time learning this.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login