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="" %>
<%@ taglib prefix="fmt" uri="" %>
<%@ taglib prefix="ora" uri="orataglib" %>
<c:set var="isValid" ...

Get JavaServer Pages, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.