Jsp: Exception handler issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robtyketto
    New Member
    • Nov 2006
    • 108

    Jsp: Exception handler issues

    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:-
    Code:
    <%@ page errorPage="EditExceptionHandler.jsp" %>
    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 EditExceptionHa ndler.jsp is below:-

    Code:
    <%@ page errorPage="ExceptionHandler.jsp" %>
    
    <html>
    <%-- Form Handler Code --%>
    <a href="wk465682EditFAQ.jsp"> Please try again </a>	
    <%
    	int faqSequence;
    		try{
    			faqSequence = Integer.parseInt(request.getParameter("faqSequence"));
    			}
    	catch (NumberFormatException e)
    		{ 
    			throw new JspException("Please enter a valid integer value !!");
    		}
    		
    %>
    </html>
    The code for the ExceptionHandle r.jsp is as follows:-

    Code:
    <%@ page isErrorPage="true" import="java.io.*" %>
    <html>
    <head>
    	<title>Exceptional Event Occurred!</title>
    	<style>
    	body, p { font-family:Tahoma; font-size:10pt;
            padding-left:30; }
    	pre { font-size:8pt; }
    	</style>
    </head>
    <body>
    <%-- Exception Handler --%>
    <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>
    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 ExceptionHandle r.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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    Working...