This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Page Directive errorPage and isErrorPage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Page Directive errorPage and isErrorPage" Watch "Page Directive errorPage and isErrorPage" New topic
Author

Page Directive errorPage and isErrorPage

Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
I have this two JSPs,

1st JSP (Hello.jsp):

<%@ page errorPage="errorHandler.jsp" %>
<html>
<body>
<%
if (request.getParameter("name") == null){
throw new RuntimeException("Name Not Specified");
}
%>
Hello, <%= request.getParameter("name") %>
</body>
</html>


2nd JSP (errorHandler.jsp):

<%@ page isErrorPage="true" %>
<html>
<body>
Unable to process your request <br>
Please try again.
</body>
</html>

However, upon running Hello.jsp with "name" parameter equal to null, I always got the message "The page cannot be displayed" in the browser. Why didn't it showed errorHandler.jsp? I know I have trapped the null value.


"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
When u don't enter any value in the TextBox and make a submit, what u get in the server is just an empty string not a null. Try with this code

<%
String inputRequest = request.getParameter("name");
if (inputRequest.equals("")){
throw new RuntimeException("Name Not Specified");
}
%>

You will endup in your errorHandler.jsp page.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Vishnu Prakash,

I tried doing your code but I get still get the same error.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi Rex,

Are you using IE ? IE does not display the customized error Page. It display its own page.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

I tried doing your code but I get still get the same error.



I gave you the code only after executing it. It worked for me.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Are you using IE ? IE does not display the customized error Page. It display its own page.


Try using the latest version IE. It works fine for me.
Anil Sharma
Greenhorn

Joined: Nov 07, 2005
Posts: 15
Hi Richard,

When I put both of these files in same folder.
I do not get any error that you mentioned.
all things are working fine, it is showing the contents the errorHandler.jsp.


Anil Sharma<br />SCJP 1.4, SCWCD 1.4(Preparing)
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Narendra Dhande, Vishnu Prakash and Anil Kumar Sharma,

I am using Internet Explorer version 6.0 with SP2. As far as I know this is the latest version of MS IE. Nonetheless, if there is a newer version than what I have now, can't it run in IEv6.0? By the way, the error I'm getting was 500 Internal Server Error. Please advise.
Jun Liu
Greenhorn

Joined: Nov 15, 2005
Posts: 11
Have you added the element <error-page> in web.xml?
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>...</location>
</error-page>
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi,

Do we need to really add the element <error-page>in web.xml to redirect to the error pages if an uncaught exception?I dont think its required.

Its not mentioned anywhere about tht in HFJS.

Also,I have tried an example like this:

index.jsp
<%@ page errorPage = "error.jsp" %>
<html>
<%--
name doent exist as request parameter so request.getParameter("name") returns null
%-->
<%= request.getParameter("name").trim()%>
</html>

error.jsp
<html>
Error Page
</html>

The above example works fine even if I dont have any <error-page> element declared in web.xml

But the whole problem comes when we modify the error.jsp as follows:

error.jsp
<%@ page isErrorPage = "true" %>
<html>
Error Page
</html>

In this case instead of redirecting to error.jsp, Page not found error is displayed.

So I think the problem is somewhere with the isErrorPage attribute.

Anyone please clarify.


Regards,
Vineela
[ November 15, 2005: Message edited by: Vineela Devi ]
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi,

Huh! I still get the same error even if I place an error tag in web.xml.
Radhika Jonnalagadda
Ranch Hand

Joined: Oct 13, 2004
Posts: 89
Hey Richard,
If it doesn't seem silly, try the code removing "<body> <html> </body> </html>" from the errorHandler.jsp.


best regards,<br />Radhika<br /> <br />Dare to Dream.Care to Achieve.<br />SCJP 1.4, SCWCD 1.4.
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
Radhika,
It shouldn't make any difference in having <html> tags.. There are no restrictions for error page.

Richard,
I guess, it is something obvious that you are missing.
we dont need to have error-page declaration in web.xml.
Make sure that you have both files in same directory.
Can you let us know, which web container are you using and which version?
When I copied your jsp pages and ran in was4.0 it worked me straight away! It displays error message to me.


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Rizwan,

I am using Tomcat version 5. My 2 jsp's are in the same directory. Both are under my root <chapter10>.

Directory structure:
webapps/chapter10/<this is where the 2 jsps are located>

I know there's nothing wrong with my codes in jsp. Is this because of my IE? I'm using IEv6.0. Will I have to change any of its settings if there is?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Under internet options in IE, make sure the option to display friendly http errors is not checked.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

When setting isErrorPage to true, the status of the http response is set to SC_INTERNAL_SERVER_ERROR.
As somebody said previously, it probably is a problem with the way your browser treats the SC_INTERNAL_SERVER_ERROR.
[ November 27, 2005: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Bosun Bello,

Thanks you very much! It did work now! I removed the checked for the friendly error message in IE settings.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
It's very helpful to know that the errorPage attribute does not work correctly with the default settings in the most popular browser. This means that in practice some other means of using a custom error page should be employed. Thanks for the tip, Bosun.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Originally posted by Joseph Sondow:
It's very helpful to know that the errorPage attribute does not work correctly with the default settings in the most popular browser.


Actually, errorPage works. The problem is isErrorPage. It sets the status code to 500, and IE overrides its behaviour. You'll have to think of something if you're using isErrorPage.
[ November 27, 2005: Message edited by: Satou kurinosuke ]
Celso Barreto
Greenhorn

Joined: Dec 14, 2005
Posts: 1
Since almost always you can't talk to all your costumer to uncheck this option at IE, insert the following line at the start of you JSP error page:

response.setStatus(javax.servlet.http.HttpServletResponse.HttpServletResponse.SC_OK);

It will change the status code response and make IE think that it is just a normal page. It's ugly but works!


Sun Certified Java Programmer 1.2<br />Sun Certified Java Programmer 1.4<br />Sun Certified Java Associate<br />Sun Certified Java Developer<br />Sun Certified Web Components Developer<br />Sun Certified Business Components Developer<br />Sun Certified Enterprise Architect part I
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
Thank you guys... disabling friendly error page display in IE solved my one day worry...
Prakash Mani - Attur
Ranch Hand

Joined: Oct 08, 2009
Posts: 100
sending success code SC_OK for error page is not good idea na.. but your point is valid.. we cant ask every client to disable that option under IE.. is there any other perfect solution???
 
jQuery in Action, 2nd edition
 
subject: Page Directive errorPage and isErrorPage
 
Similar Threads
Question on Page Directive
body-content tag in a TLD
regarding page directive: errorPage
jsp question - errorPage
Want my exceptions to be caught by errorPage JSP ?