Try Catch & error.jsp

I am working with jsp and have page called error.jsp.  I am hoping to make use of the error.jsp page.  What I want is to tell the user what went wrong then also have the stack trace show below.  (being new to programming I learned to love stack traces therefore desire to have it appear on the error.jsp page).  IFound below is my error.jsp.

I am hoping that you can provide a simple piece of code with an error (try /catch) that I can place in any of my jsp pages. when the page loads the error statement and stack trace will be passed to the error.jsp page so that the user will know why it failed.  please provide suggestions for altering code below such that it will accomidate your sample.  I do appreciate your assistance!!
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Error Finder</title>
</head>

<body>
<script language="JavaScript1.2" src="http://internal.baxter.com/js/header/nbtheader.js"></script>
<table width="800" border="0">
    <tr> 
      <td><jsp:include page="includes/cfobheader.html" flush="true" />
      </td>
    </tr><tr>
				<td align="right" valign="bottom"> <h1 align="left"> </h1> </td> 
	</tr>
	<tr> 
		<td align="right"> </td>
	</tr> 
	<tr> 
		<td align="right"><div align="left">Please click the back arrow in an effort to fix the error found below. </div></td>
	</tr>	
<tr>
</tr>
<tr>
		<td align="right">&nbsp;  </td>
	</tr> 


	<tr> 
		<td align="right"><div align="left">The name of the exception is
	      <%= exception.toString() %>. </div></td>
		
	</tr>

	<tr>
		<td align="right">&nbsp;  </td>
	</tr> 
	
</table> 
<script language="JavaScript1.2" src="http://internal.baxter.com/js/header/footer.js"></script>
</body>
</html>

Open in new window

LVL 3
gazdzidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

afibarraCommented:
Try adding this code to your errorPage:

<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>
0
gazdzidAuthor Commented:
can you provide a piece of code that will generate an error (for example when page loads) I would like error as a means to demonstrate fuctionality of
your code?

0
afibarraCommented:
Try this 3 files (code added below):

1. ageForm.html
2. ageFormHandler.jsp
3. exceptionHandler.jsp
ageForm:

<html>
<head>
  <title>Age Form</title>
</head>
<body>

<form action="ageFormHandler.jsp" method="post">
	Age: 
	<input type="text" name="age" /> 
	<input type="submit" value="Submit" />
</form>

</body>
</html>

ageFormHandler:

<%@ page errorPage="exceptionHandler.jsp" %>
<html>
<head>
</head>
<body>

<%
	int age;

		age = Integer.parseInt(request.getParameter("age"));
%>

<p>Your age is : <%= age %> years.</p>

<p><a href="ageForm.html">Back</a>.</p>

</body>
</html>

exceptionHandler:

<%@ page isErrorPage="true" import="java.io.*" %>
<html>
<head>
	<title>Error!</title>
</head>
<body>

<font color="red">
<%= exception.toString() %><br>
</font>

<%
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");
%>

</body>
</html>

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

gazdzidAuthor Commented:
Thanks, i will test code then assign points probably tomorrow.
0
afibarraCommented:
yw =)
0
gazdzidAuthor Commented:
afibarra:

Sorry for the delay and thanks for the suggestion, which seems to address half of the initial question, which is returning the error statement.

java.lang.NumberFormatException: For input string: "a"

could you also provide the code that would produce the stack trace?  with your response my returned error would be as follows:

java.lang.NumberFormatException: For input string: "a

===================================================

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /aa_ageFormHandler.jsp:11

8: <%
9:         int age;
10:  
11:                 age = Integer.parseInt(request.getParameter("age"));
12: %>
13:  
14: <p>Your age is : <%= age %> years.</p>


Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NumberFormatException: For input string: "a"
      java.lang.NumberFormatException.forInputString(Unknown Source)
      java.lang.Integer.parseInt(Unknown Source)
      java.lang.Integer.parseInt(Unknown Source)
      org.apache.jsp.aa_005fageFormHandler_jsp._jspService(aa_005fageFormHandler_jsp.java:52)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.

0
afibarraCommented:
Hi,

Line 56 "exception.printStackTrace(pw);" of the code above gives you the stacktrace
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gazdzidAuthor Commented:
I did the following:

      1.   Changed your title to error.jsp.
      2.     Added <%@ page errorPage="error.jsp" %> to a jsp page that inserts a record.
      3.    Entered a duplicate record as a means to generate a unique constraint error.

Results are as follows:
                 
                  Please click the back arrow in an effort to fix the error found below.  

   
java.sql.SQLException: ORA-00001: unique constraint (CFOB_ELDB.DURATION_TAB_UK_I01) violated
 
Did not see the stack trace can you tell me why?
           

<%@ page isErrorPage="true" import="java.io.*" %> 
<html>
<head>
<title>Error Finder</title>
</head>

<body>
<script language="JavaScript1.2" src="http://internal.baxter.com/js/header/nbtheader.js"></script>
<table width="800" border="0">
    <tr> 
      <td><jsp:include page="includes/cfobheader.html" flush="true" />
      </td>
    </tr><tr>
				<td align="right" valign="bottom"> <h1 align="left"> </h1> </td> 
	</tr>
	<tr> 
		<td align="right"> </td>
	</tr> 
	<tr> 
		<td align="right"><div align="left">Please click the back arrow in an effort to fix the error found below. </div></td>
	</tr>	
<tr>
</tr>
<tr>
		<td align="right">&nbsp;  </td>
	</tr> 
<tr>
<td>
	<font color="red"> 
<%= exception.toString() %><br> 
</font> </td>
 </tr>
<% 
out.println("<!--"); 
StringWriter sw = new StringWriter(); 
PrintWriter pw = new PrintWriter(sw); 
exception.printStackTrace(pw); 
out.print(sw); 
sw.close(); 
pw.close(); 
out.println("-->"); 
%> 

<tr>
	
</table> 
<script language="JavaScript1.2" src="http://internal.baxter.com/js/header/footer.js"></script>
</body>
</html>

Open in new window

0
gazdzidAuthor Commented:
If i change the line of code from " out.print(pw); " To System.out.print(pw);

The following is returned ( i am expecting to see what is showing in the code section)


java.sql.SQLException: ORA-00001: unique constraint (CFOB_ELDB.DURATION_TAB_UK_I01) violated at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745) at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219) at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:970) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1190) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3370) at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3454) at org.apache.jsp.a_005fadd_005fduration_jsp._jspService(a_005fadd_005fduration_jsp.java:196) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) at java.lang.Thread.run(Unknown Source) java.io.PrintWriter@3fc47c-->
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: Exception in JSP: /a_add_duration.jsp:113

110:     Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();
111:     Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);
112:     PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());
113:     MM_editStatement.executeUpdate();
114:     MM_connection.close();
115: 
116:     // redirect with URL parameters


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause 

javax.servlet.ServletException: ORA-00001: unique constraint (CFOB_ELDB.DURATION_TAB_UK_I01) violated

	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
	org.apache.jsp.a_005fadd_005fduration_jsp._jspService(a_005fadd_005fduration_jsp.java:325)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause 

java.sql.SQLException: ORA-00001: unique constraint (CFOB_ELDB.DURATION_TAB_UK_I01) violated

	oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
	oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
	oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
	oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
	oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
	oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:970)
	oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1190)
	oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3370)
	oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3454)
	org.apache.jsp.a_005fadd_005fduration_jsp._jspService(a_005fadd_005fduration_jsp.java:196)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.

Open in new window

0
gazdzidAuthor Commented:
Sorry afibarra:

Only when I change

out.println("<!--");

to


System.out.println("<!--");

Then  I see return as stated above (not in code section, which is the desired)
0
gazdzidAuthor Commented:
Thanks, If you could respond to my previous comment that would be great.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.