Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Page Handling

 
Parvinder singh Palia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Parvinder singh Palia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot Sir!

" You Are The Man "
Thanks for the answer.
Long Live Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic