• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • Last Modified:

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

0
gazdzid
Asked:
gazdzid
  • 7
  • 4
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now