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 action 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 12-8.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="ora" uri="orataglib" %>
<c:set ...
Get JavaServer Pages, 3rd 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.