Validating Complex Input Without a Bean
Before we
look at the two remaining database
sections, let’s go back and take a look at the two
application pages we skipped earlier, namely the
enter.jsp
and validate.jsp
pages used for input to the employee registration.
In Chapter 8, I introduced you to validation of
user input using the JSTL <c:if>
action as
well as using an application-specific bean. The bean contains all
validation code and can therefore validate the format of complex
data, such as date strings, email addresses, and credit-card numbers.
This is the approach I recommend, but if you’re
developing a JSP-based application without access to a Java
programmer to develop the beans you need, I’ll show
you a trick you can use to validate dates and a custom action for
email-address validation.
The validate.jsp
page uses the JSTL
<c:if>
action and the custom actions to
validate all user input. If an input parameter isn’t
valid, an error message is saved in a variable, and the request is
forwarded back to the enter.jsp
page. The
enter.jsp
page adds all the error messages to
the response, so to the user, the result is identical to the
bean-based validation approach you saw in Chapter 8.
Let’s look at
validate.jsp
first,
shown in Example 11-8.
Example 11-8. Validation with application beans (validate.jsp)
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="ora" uri="orataglib" %>
<c:set var="isValid" ...
Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.