Connecting Tech Pros Worldwide Forums | Help | Site Map

Jsp: Exception handler issues

Member
 
Join Date: Nov 2006
Posts: 108
#1: Mar 26 '08
Greetings,

I'm a newbie to jsp/java so apologies if this request seems trivial.

I have two forms (separate .jsps) which are used to manipulate database records, one adds a record the others update an existing record.

As all fields on the HTML form are text I need to ensure those fields that should be integer values are validated, therefore I include an errorpage (The form action is used to call .jsps needed for the database actions) which acts as a form handler:-
Expand|Select|Wrap|Line Numbers
  1. <%@ page errorPage="EditExceptionHandler.jsp" %>
  2.  
on both the add and edit record .jsps (changing the errorpage accordingly)

Which checks to ensure the sequence field really is an integer, the code for the EditExceptionHandler.jsp is below:-

Expand|Select|Wrap|Line Numbers
  1. <%@ page errorPage="ExceptionHandler.jsp" %>
  2.  
  3. <html>
  4. <%-- Form Handler Code --%>
  5. <a href="wk465682EditFAQ.jsp"> Please try again </a>    
  6. <%
  7.     int faqSequence;
  8.         try{
  9.             faqSequence = Integer.parseInt(request.getParameter("faqSequence"));
  10.             }
  11.     catch (NumberFormatException e)
  12.         { 
  13.             throw new JspException("Please enter a valid integer value !!");
  14.         }
  15.  
  16. %>
  17. </html> 
The code for the ExceptionHandler.jsp is as follows:-

Expand|Select|Wrap|Line Numbers
  1. <%@ page isErrorPage="true" import="java.io.*" %>
  2. <html>
  3. <head>
  4.     <title>Exceptional Event Occurred!</title>
  5.     <style>
  6.     body, p { font-family:Tahoma; font-size:10pt;
  7.         padding-left:30; }
  8.     pre { font-size:8pt; }
  9.     </style>
  10. </head>
  11. <body>
  12. <%-- Exception Handler --%>
  13. <font color="red">
  14. <%= exception.toString() %><br>
  15. </font>
  16. <%
  17. out.println("<!--");
  18. StringWriter sw = new StringWriter();
  19. PrintWriter pw = new PrintWriter(sw);
  20. exception.printStackTrace(pw);
  21. out.print(sw);
  22. sw.close();
  23. pw.close();
  24. out.println("-->");
  25. %>
  26. </body>
  27. </html>
As I have two transactions that require validation for the exact same error how can I combine the functionality?

At the moment when the error is displayed the anchor tag and href isn’t visible (though it is when I add the code to the ExceptionHandler.jsp), I assumed even though it passes the new Exception on it would still display the link.

Then the other issue I have is how can I specify if the error is from XXX.jsp (add or edit .jsp) then return to the same XXX.jsp if I only use one .jsp error handler?

Hope this makes sense.

Thanks
Rob

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Mar 26 '08

re: Jsp: Exception handler issues


I suggest that you use Javascripts for the input validations. You can even disable any non-numerical characters from being entered into the fields at all.
It's quicker and thus more user friendly.

To determine which JSP the request is coming from, you can use a hidden parameter in the JSPs with the same name but different values in the different JSPs. Then you simply test for the value of that parameter.
Reply