FT228/3 Web Development Error processing
Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and fix syntax errors 2) Deal with runtime errors
Some Tips on errors and debugging in JSP Typical errors are 1. - wrong or missing brackets (e.g. ${ …} for EL) 2. - Tags not closed either with a closing tag (e.g. …. OR if an empty element 3. taglib directive missing tags won’t be recognised 4.“ “ incorrectly used when setting values of an action
Some Tips on errors and debugging in JSP Compilation errors generated by Apache Tomcat vary in helpfulness, depending on the error If location of error is given e.g. ……. /ch9/error.jsp (0, 33) unterminated tag The first index (0) refers to the line number in the JSP page, 0 is the first line number. The second index “33” refers to the position within the line. Not all errors are so meaningful To debug a JSP, can use actions to output values if you need to see the values of variables
Runtime errors In java – have used try/catch blocks to trap errors gracefully Two techniques are useful in JSP (1) Catch exceptions (similar to java). (2) Using error pages - your application will default to an error page specified by you, if an uncaught exception is encountered
Runtime errors – catching exceptions JSTL provides a action to catch exceptions that occur when a JSP page is run Similar to java.. but no ‘try’ needed is placed around the code that is likely to cause the error The tag provides an attribute “var” which you must name in order to hold the error message. Example: Error msg put in here if error thrown
Runtime errors – catching exceptions To then test if an error has occurred… just check the contents of your error message variable: Example: Checks the error variable to see if an error occurred
JSP page using c:catch Enter two numbers and click ‘divide’ to get result of number 1 / number 2
Please enter numbers Number 1: Number 2: JSP page using c:catch - continued overleaf
JSP page using c:catch
doesn’t allow you to specify a specific exception – just allows errors to be handled gracefully within the page Prevents an un-userfriendly exception stack trace from being displayed to the user JSP page using c:catch
Error page Can specify a default error page for your application When an unhandled error occurs (i.e. not handled by c:catch, this page will be displayed Use page directive attribute “errorpage” to specify a default error page that should be display each time an unhandled runtime error occurs page errorPage = “errorpage.jsp”
Error page Must have error page (e.g errorpage.jsp) available. This error page must declare itself as an error, also using page directive: page iserrorPage = “true”
Runtime errors Using JSTL – Runtime errors are less likely. JSTL tries to anticipate or handle the error gracefully.. e.g. if Divide by zero error is encountered – JSTL will simply print out a result as “Divide by zero” To find the runtime errors that may occur, test your code!
Anticipate errors Design your code so that errors are anticipated and managed before they occur E.g. User registration – adding a new user. User is identified by username which they enter before adding the user, check whether the username has already been used before..how?