Title: FT2283 Web Development
1FT228/3 Web Development
Error processing
2Introduction
- READ Chapter 9 of Java Server Pages from Oreilly
2nd Edition - Need to be able to
- 1) Diagnose and fix syntax errors
- 2) Deal with runtime errors
3Some Tips on errors and debugging in JSP
- Typical errors are
- - wrong or missing brackets (e.g. for EL)
- - Tags not closed either with a closing tag
(e.g. ltcifgt.lt/cifgt - OR if an empty element ltcout . /gt
- 3. taglib directive missing ? tags wont be
recognised - incorrectly used when setting values of an
action -
4Some 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 lt_at_ - 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 meaningfulTo
debug a JSP, can use ltcoutgt actions to output
values if you need to see the values of
variables
5Runtime 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
6Runtime errors catching exceptions ltccatchgt
- JSTL provides a ltccatchgt action to catch
exceptions that occur when a JSP page is run - Similar to java .. but no try needed ?
ltccatchgt is placed around the code that is
likely to cause the error - The ltccatchgt tag provides an attribute var
which you must name in order to hold the error
message. - Example
- ltccatch var "errormsg"gt
- ltcout value "The result of your calculation
is param.num1/param.num2"/gt - lt/ccatchgt
Error msg put in here if error thrown
7Runtime errors catching exceptions ltccatchgt
- To then test if an error has occurred just check
the contents of your error message variable - Example
- ltccatch var "error"gt
- ltcout value "The result of your calculation
is param.num1/param.num2"/gt - lt/ccatchgt
-
- ltcif test "error!null"gt
- ltcout value "You have an error
error"/gt - lt/cifgt
Checks the error variable to see if an error
occurred
8JSP page using ccatch
Enter two numbers and click divide to get
result of number 1 / number 2
9JSP page using ccatch
lt_at_ page contentType "text/html" gt lt_at_ taglib
prefix "c" uri"http//java.sun.com/jstl/core"
gt lthtmlgt ltbodygt ltform method "post" action
"catch_example.jsp"gt Please enter numbers
ltbrgt ltinput type hidden name
submitted value "true"/gt Number 1 ltInput
type text Name "num1" gtltbrgt Number 2
ltInput type text Name "num2"gtltbrgt
-? continued overleaf
10JSP page using ccatch
ltinput type "submit" value
"divide"gt lt/formgt ltcif test "!empty
param.submitted"gt ltccatch var "error"gt
ltcout value "The result of your
calculation is param.num1/param.num2"/gt
lt/ccatchgt ltcif test
"error!null"gt ltcout value "You have
an error error"/gt lt/cifgt lt/cifgt
lt/bodygt lt/htmlgt
11JSP page using ccatch
- ltccatch gt doesnt 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
12Error page
- Can specify a default error page for your
application - When an unhandled error occurs (i.e. not handled
by ccatch, 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 - lt_at_ page errorPage errorpage.jsp
13Error page
- Must have error page (e.g errorpage.jsp)
available. - This error page must declare itself as an error,
also using page directive
- lt_at_ page iserrorPage true
14Runtime 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 zeroTo find the runtime errors
that may occur, test your code!
15Anticipate 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?