jk.hiren
jk.hiren

Reputation: 1

Configuring single jsp error page with different messages for different http errors

I am developing an web-application.

I am looking to configure error pages in web.xml.

I want to make a single error page with different error messages for different http errors like 400,404,etc.

For example : I want to use the same error page "error.jsp" for all http errors. assume if error 400 arises, jsp should display "Bad request" and if error 404 occurs,jsp should display messgae as "page not found".

please help.

Thanks.

Upvotes: 0

Views: 683

Answers (1)

verisimilitude
verisimilitude

Reputation: 5110

This in fact is possible and is fairly simple. I will post 3 ways here.

1) The Servlets 3.0 way: Provided that you have upgraded to the latest Servlets 3.0, you could very easily do this by omitting the <error-code> or exception-type element of <error-page> and just provide the <location> and create a default error.jsp for your application. `

2) Explicitly type in all the 400s and 500s and map them to your error.jsp in your web.xml; This is in case you are using an older container and don't wish to upgrade. There are not many of them

3) There is a third way here (@BalusC's answer, I admit I respect that guy for his knowledge and his reputation on SO)

To output error-code specific messages in your error.jsp use ${requestScope['javax.servlet.error.status_code']} either in a switch-case or an if-else-if construct.

Upvotes: 1

Related Questions