Java Servlet & JSP Cookbook

Problem

You want to set a request-, session-, or application-scoped attribute to a value that a client has submitted as part of form input.

Solution

Use the jsp:useBean and jsp:setProperty standard actions to set a JavaBean's property to the submitted value. Then use the c:set JSTL custom tag to set the attribute to the validated value.

Discussion

Some web applications may validate form input such as an email/password combination, then set a request-, session-, or application-scoped attribute to the validated value. An efficient way to handle important data that a user submits is to use a JavaBean whose purpose is to validate the submission against some business rule or external resource, such as a database. If the submission is valid, then the application creates a session attribute, for instance, with the value. If the submission is invalid, then a boolean variable in the JavaBean is set to false . The JSP to which the form input is sent can check this value before it handles the data as valid.

Example 7-7 shows a ClientValidator bean that has three fields: email , password , and valid . This bean is used by a JSP to validate form input before the JSP sets request-scoped attributes to the submitted values.

Example 7-7. The ClientValidator bean

package com.jspservletcookbook; public class ClientValidator implements java.io.Serializable{ String email; String password; boolean valid; public ClientValidator( ){ this.valid=false;} public boolean isValid( ){ /* Use a Data Access Object to validate the email and password. If the validation does not fail, then set this.valid to true*/ this.valid=true; return valid; } public void setEmail(String _email){ if(_email != null && _email.length( ) > 0) email = _email; else email = "Unknown"; } public String getEmail( ){ return email; } public void setPassword(String _password){ if(_password != null && _password.length( ) > 0) password = _password; else password = "Unknown"; } public String getPassword( ){ return password; } }

Example 7-8 is the JSP that uses ClientValidator . The JSP first uses jsp:useBean to create an instance of the bean. Then it sets the fields or properties of the bean to the values that have been posted to the JSP, which are "email" and "password". If the isValid bean property is true , which is tested with this JSTL code:

<c:if test="${isValid}">

then the JSP sets two request-scoped attributes. The attributes are now available to a page that is forwarded this request. Session attributes are accessible from servlets and JSPs that are associated with the same session (see Chapter 11). The application scope encompasses the context or web application.

If you want to set session- or application-scoped attributes, change the code in Example 7-8 to:

<c:set var="email" value="${chk.email}" scope="session" /> <c:set var="password" value="${chk.password}" scope="session" />

or:

<c:set var="email" value="${chk.email}" scope="application" /> <c:set var="password" value="${chk.password}" scope="application" />

Example 7-8. validChk.jsp page that uses a validator bean to check form input data

<%@page contentType="text/html"%> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <jsp:useBean id="chk" class="com.jspservletcookbook.ClientValidator" > <jsp:setProperty name="chk" property="*" /> </jsp:useBean> <%-- get valid property from ClientValidator bean --%> <c:set var="isValid" value="${chk.valid}" /> <c:if test="${isValid}"> <c:set var="email" value="${chk.email}" scope="request" /> <c:set var="password" value="${chk.password}" scope="request" /> </c:if> <html> <head><title>Client Checker</title></head> <body> <h2>Welcome</h2> <strong>Email</strong>: <c:out value="${email}" /><br><br> <strong>Password</strong>: <c:out value="${password}" /> </body> </html>

See Also

Recipe 7.2 on handling a POST request in a JSP; Recipe 7.3 on setting the properties of a JavaBean to form input; Recipe 7.6 on posting data from a JSP; Chapter 23 on using the JSTL.

Категории