Chapter 3 Examples
Download the Chapter 3 code
View the code examples
3.1 3.2 3.3 3.4 3.5 3.6
Example 3.1 Including an All-or-Nothing Guard per View
<%@ tagline uri="/WEB-INF/corej2eetaglibrary.tld"
prefix="corePatterns" %>
<corePatterns:guard/>
<HTML>
.
.
.
</HTML>
Example 3.2 Portions of View Not Displayed Based on User Role
<%@ taglib uri="/WEB-INF/corej2eetaglibrary.tld"
prefix="corePatterns" %>
<HTML>
.
.
.
<corePatterns:guard role="manager">
<b>This should be seen only by managers!</b>
<corePatterns:guard/>
.
.
.
</HTML>
Example 3.3 Unassigned Security Role Provides All-or-Nothing Control
<security-constraint>
<web-resource-collection>
<web-resource-name>SensitiveResources
</web-resource-name>
<description>A Collection of Sensitive Resources
</description>
<url-pattern>/trade/jsp/internalaccess/
sensitive1.jsp</url-pattern>
<url-pattern>/trade/jsp/internalaccess/
sensitive2.jsp</url-pattern>
<url-pattern>/trade/jsp/internalaccess/
sensitive3.jsp</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>sensitive</role-name>
</auth-constraint>
</security-constraint>
Example 3.4 Form-Centric Validation
/**If the first name or last name fields were left
blank, then an error will be returned to client.
With this strategy, these checks for the existence
of a required field are duplicated. If this validation
logic were abstracted into a separate component,
it could be reused across forms (see
Validation Based on Abstract Types strategy)**/
public Vector validate()
{
Vector errorCollection = new Vector();
if ((firstname == null) ||
(firstname.trim().length() < 1))
errorCollection.addElement("firstname required");
if ((lastname == null) || (lastname.trim().length()
< 1))
errorCollection.addElement("lastname required");
return errorCollection;
}
Example 3.5 Validation Based on Abstract Types
//firstNameString="Dan"
//formFieldName="form1.firstname"
Validator.getInstance().validate(firstNameString,
formFieldName);
Example 3.6 Helper Properties - A Simple JavaBean Helper
public class Helper
{
private String first;
private String last;
public String getFirst()
{
return first;
}
public void setFirst(String aString)
{
first=aString;
}
public String getLast()
{
return last;
}
public void setLast(String aString)
{
last=aString;
}
}
|