Input Validation 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 5, I introduced you to validation of user input using an application-specific bean. The bean contains all validation code and provides an isValid( ) method that can be used in a JSP page to decide how to proceed. This is the approach I recommend, but if you’re developing a JSP-based application and there isn’t a Java programmer around, there’s another way to do the validation. I’ll describe this alternative here.

The validate.jsp page uses the StringFormat utility class to validate the input format without a bean. If an input parameter is not valid, an error message is saved in a Vector object and the request is forwarded back to the enter.jsp page. The enter.jsp page loops through all error messages in the Vector and adds them to the response, so to the user, the result is identical to that of the bean-based validation approach you saw in Chapter 5.

Let’s look at validate.jsp first, shown in Example 9.6.

Example 9-6. Validation Without Application Beans (validate.jsp)

<%@ page language="java" %> <%@ page import="com.ora.jsp.util.*" %> <jsp:useBean id="errorMessages" scope="request" class="java.util.Vector" /> <% if (request.getParameter("userName").length( ) == 0) { errorMessages.addElement("User Name missing"); } if ...

Get Java Server Pages 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.