aspose file tools*
The moose likes Servlets and the fly likes Error Page Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error Page Handling" Watch "Error Page Handling" New topic
Author

Error Page Handling

Parvinder singh Palia
Greenhorn

Joined: Jan 24, 2001
Posts: 5
Hi! Guys,
How to handle exception's in JSP .I mean using the
following code:
<%@ page isErrorPage="false" errorPage="errorpage.jsp"%>
And setting the "errorpage.jsp" property:
<%@ page isErrorPage="true" %>
<h1>Warning</h1>
<%=exception %>
This is what i have coded,but the main page refuses to load the
"errorpage.jsp"
Please advice.
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
You use exception object, but you never use any methods associated with this object:
- getMessage()
- printStackTrace()
- toString
Also, when you write: <%@ page isErrorPage="false" errorPage="errorpage.jsp"%>, you should change isErrorPage="false" to "true".
So, something like this would work:
<@page isErrorPage="true">
Error:<%=exception%>
<% exception.printStackTrace(out); %>

------------------
Alex
"Java is the answer"


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Parvinder singh Palia
Greenhorn

Joined: Jan 24, 2001
Posts: 5
Thanks for responding.
Firstly I used to think one has to set the "Main page.jsp" property :
<%@ page isErrorPage="false" errorPage="errorpage.jsp" %>
and only the "errorpage.jsp" property :
<%@ page isErrorPage="true" %>
So thanks for the correction.
Secondly as you suggested i have used the "object.method" but the
server gives the following error:
" Can't convert javax.servlet.jsp.JspWriter to
java.io.PrintWriter."
Please advice.

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Well, the way error pages work is that one page is throwing an error and other displays the error. For example if your index.jsp generates an exception, and at the top of the page you say <@page errorPage="error.jsp" @>, all exceptions will be forwarded to error.jsp page.
In error.jsp you say <%@page isErrorPage="true" %> since default value is "false." In error.jsp if you write:
<%= exception %>
<% exception.toString(); %> or <% exception.getMessage(); %>
it will receive these exceptions and display them.
Hope this helps
------------------
Alex
"Java is the answer"
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
here, I did a little example, you can see it at http://www.mycgiserver.com/~akravets/info2.jsp
-----------------------------------------------
there are two files:
info2.jsp:
---------
<%@ page errorPage="error_info.jsp"%>
<html>
<body>
<%
int a = 5;
int b = 0;

int c = a/b; // here I divide by zero, WRONG, will cause exception
%>
<%=c%>
</body>
</html>
--------------------------------------------------
error_info.jsp:
---------------
<%@ page isErrorPage="true" %>
<html>
<body>
The name of the exception was:<%= exception.toString() %>
The message of the exception was: <%= exception.getMessage() %>
</body>
</html>
this page catches the errors in exception object.
Parvinder singh Palia
Greenhorn

Joined: Jan 24, 2001
Posts: 5
Fine agreed by your solution.
Still there is a problem:
If i have the following code as say "main_page.jsp" :-
<jsp:useBean id="user" class="UserData" scope="session"/>
<%@ page errorPage="error_info.jsp"%>
<HTML>
<BODY>
<FORM METHOD=POST ACTION="test12.jsp">
Your name?<INPUT TYPE=TEXT NAME=username SIZE=20 ><BR>
Your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><br>
Your age? <INPUT TYPE=TEXT NAME=age SIZE=4 >
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
And The "error_info.jsp" is same as you coded.
The "error_info.jsp" does not load.
The server-error page is displayed instead.
Please advice
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
<jsp:useBean id="user" class="UserData" scope="session"/>
<%@ page errorPage="error_info.jsp"%>
<HTML>
<BODY>
<FORM METHOD=POST ACTION="test12.jsp">
Your name?<INPUT TYPE=TEXT NAME=username SIZE=20 ><BR>
Your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><br>
Your age? <INPUT TYPE=TEXT NAME=age SIZE=4 >
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
Well, here you say that your main page should see the error, but exception is only generated when you hit "submit", so try putting <%@ page errorPage="error_info.jsp"%> in your test12.jsp page, since this is where your exceptions are coming from.
------------------
Alex
"Java is the answer"
Parvinder singh Palia
Greenhorn

Joined: Jan 24, 2001
Posts: 5
Thanks a Lot Sir!

" You Are The Man "
Thanks for the answer.
Long Live Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error Page Handling
 
Similar Threads
designing error pages
problem with errorPage and isErrorPage="true"
error pages in dd
Problem configuring Error Page
Error-Code Tag of web.xml 2.4 not working