The following sections describe how to use WebLogic JSP form validation tags:
WebLogic JSP form validation tags provide a convenient way to validate the entries an end user makes to HTML form text fields generated by JSP pages. Using the WebLogic JSP form validation tags prevents unnecessary and repetitive coding of commonly used validation logic. The validation is performed by several custom JSP tags that are included with the WebLogic Server distribution. The tags can
WebLogic JSP form validation tags include:
When a validation tag determines that data in a field is not been input correctly, the page is re-displayed and the fields that need to be re-entered are flagged with text or an image to alert the end user. Once the form is correctly filled out, the end user’s browser displays a new page specified by the validation tag.
This section describes the WebLogic form validation tags and their attributes. Note that the prefix used to reference the tag can be defined in the taglib
directive on your JSP page. For clarity, the wl
prefix is used to refer to the WebLogic form validation tags throughout this document.
<wl:summary>
is the parent tag for validation. Place the opening <wl:summary>
tag before any other element or HTML code in the JSP. Place the closing </wl:summary>
tag anywhere after the closing </wl:form>
tag(s).
name
<wl:validator>
tags on the JSP page. If you do not define this attribute, the default value, errorVector
, is used. The text of the error message is defined with the errorMessage
attribute of the <wl:validator>
tag.
<wl:errors/>
tag. To use the <wl:errors/>
tag, place the tag on the page where you want the output to appear. For example:
<wl:errors color="red"/>
Alternately, you can use a scriptlet. For example:
<% if (
errorVector
.size() > 0) {
for (int i=0; i <
errorVector
.size(); i++) {
out.println((String)
errorVector
.elementAt(i));
out.println("<br>");
}
} %>
Where errorVector
is the name of the vector assigned using the name
attribute of the <wl:summary>
tag.
The name
attribute is required when using multiple forms on a page.
headerText
<% if(
summary
.size() >0 ) {
out.println(headerText);
}
%>
Where summary
is the name of the vector assigned using the name
attribute of the <wl:summary>
tag.
redirectPage
action
attribute of the <wl:form>
tag.
redirectPage
attribute to the same page containing the <wl:summary>
tag—you will create an infinite loop causing a StackOverFlow
exception.
The <wl:form>
tag is similar to the HTML <form>
tag and defines an HTML form that can be validated using the WebLogic JSP form validation tags. You can define multiple forms on a single JSP by uniquely identifying each form using the name
attribute.
method
action
redirectPage
attribute of the <wl:summary>
tag and is useful if you have multiple forms on a single JSP page.
action
attribute to the same page containing the <wl:form>
tag—you will create an infinite loop causing a StackOverFlow
exception.
name
<form>
tag. Identifies the form when multiple forms are used on the same page. The name
attribute is also useful for JavaScript references to a form.
Use one or more <wl:validator>
tags for each form field. If, for instance, you want to validate the input against a regular expression and also require that something be entered into the field you would use two <wl:validator>
tags, one using the RequiredFieldValidator class and another using the RegExpValidator class. (You need to use both of these validators because blank values are evaluated by the Regular Expression Field Validator as valid.)
errorMessage
name
attribute of the <wl:summary>
tag.
expression
fieldToValidate
name
attribute of the HTML <input>
tag.
validatorClass
fieldToValidate
attribute to the two fields you want to compare. For example:
To use a validation tag in a JSP:
taglib
directive to reference the tag library containing the WebLogic JSP Form Validation Tags. For example:<%@ taglib uri="tagl" prefix="wl" %>
Note that the prefix attribute defines the prefix used to reference all tags in your JSP page. Although you may set the prefix to any value you like, the tags referred to in this document use the wl
prefix.
<wl:summary> ... </wl:summary>
tags.
Place the opening <wl:summary ...>
tag before any HTML code, JSP tag, scriptlet, or expression on the page.
Place the closing </wl:summary>
tag anywhere after the </wl:form>
tag(s).
<wl:form>
JSP tag that is included with the supplied tag library. For more information, see
<wl:form> on page 3 and
Creating HTML Forms Using the <wl:form> Tag on page 6. Be sure to close the form block with the </wl:form>
tag. You can create multiple forms on a page if you uniquely define the name
attribute of the <wl:form>
tag for each form.<input>
tag.<wl:validator>
tags. For the syntax of the tags, see
<wl:validator> on page 4. Place <wl:validator>
tags on the page where you want the error message or image to appear. If you use multiple forms on the same page, place the <wl:validator>
tag inside the <wl:form>
block containing the form fields you want to validate. The following example shows a validation for a required field:
<wl:form name="FirstForm" method="POST" action="thisJSP.jsp">
<wl:validator
errorMessage="Field_1 is required" expression=""
fieldToValidate="field_1"
validatorClass=
"weblogicx.jsp.tags.validators.RequiredFieldValidator"
>
<img src="images/warning.gif">
<font color=red>Field 1 is a required field</font>
</wl:validator>
<p> <input type="text" name = "field_1"> </p>
<p> <input type="text" name = "field_2"> </p>
<p> <input type="submit" value="Submit FirstForm"> </p>
</wl:form>
If the user fails to enter a value in field_1
, the page is redisplayed, showing a warning.gif
image, followed by the text (in red) “Field 1 is a required field,
” followed by the blank field for the user to re-enter the value.
weblogic-vtags.jar
file from the ext
directory of your WebLogic Server installation into the WEB-INF/lib
directory of your Web Application. You may need to create this directory.taglib
element to the web.xml
deployment descriptor for the Web Application. For example:<taglib>
<taglib-uri>tagl</taglib-uri>
<taglib-location>
/WEB-INF/lib/weblogic-vtags.jar
</taglib-location>
</taglib>
This section contains information on creating HTML forms in your JSP page. You use the <wl:form>
tag to create a single form or multiple forms on a page.
Use the <wl:form>
tag that is provided in the weblogic-vtags.jar
tag library: For example:
<wl:form method="POST" action="nextPage.jsp">
<p> <input type="text" name ="field_1"> </p>
<p> <input type="text" name ="field_2"> </p>
<p> <input type="submit" value="Submit Form"> </p>
</wl:form>
For information on the syntax of this tag see <wl:form> on page 3.
When using multiple forms on a page, use the name
attribute to identify each form. For example:
<wl:form name="FirstForm" method="POST" action="thisJSP.jsp">
<p> <input type="text" name="field_1"> </p>
<p> <input type="text" name="field_2"> </p>
<p> <input type="submit" value="Submit FirstForm"> </p>
</wl:form>
<wl:form name="SecondForm" method="POST" action="thisJSP.jsp">
<p> <input type="text" name="field_1"> </p>
<p> <input type="text" name="field_2"> </p>
<p> <input type="submit" value="Submit SecondForm"> </p>
</wl:form>
When the JSP page is re-displayed after the validator tag has found errors, it is useful to re-display the values that the user already entered, so that the user does not have to fill out the entire form again. Use the value
attribute of the HTML <input>
tag or use a tag library available from the Apache Jakarta Project. Both procedures are described next.
You can use the javax.servlet.ServletRequest.getParameter()
method together with the value
attribute of the HTML <input>
tag to re-display the user’s input when the page is re-displayed as a result of failed validation. For example:
<input type="text" name="field_1"
value="<%= request.getParameter("field_1") %>" >
To prevent cross-site scripting security vulnerabilities, replace any HTML special characters in user-supplied data with HTML entity references. For more information, refer to JSP Expression Language.
You can also use a JSP tag library available free from the Apache Jakarta Project, which provides the <input:text>
tag as a replacement for the HTML <input>
tag. For example, the following HTML tag:
<input type="text" name="field_1">
could be entered using the Apache tag library as:
<input:text name="field_1">
For more information and documentation, download the Input Tag library.
To use the Apache tag library in your JSP:
input.jar
file from the Input Tag Library distribution file into the WEB-INF/lib
directory of your Web Application.<%@ taglib uri="input" prefix="input" %>
web.xml
deployment descriptor of your Web application:<taglib>
<taglib-uri>input</taglib-uri>
<taglib-location>/WEB-INF/lib/input.jar</taglib-location>
</taglib>
To use your own validator class:
weblogicx.jsp.tags.validators.CustomizableAdapter
abstract class. For more information, see
Extending the CustomizableAdapter Class on page 9.validate()
method. In this method:.class
file in the WEB-INF/classes
directory of your Web application.<wl:validator>
tag by specifying the class name in the validatorClass
attribute. For example:<wl:validator errorMessage="This field is required" fieldToValidate="field_1" validatorClass="mypackage.myCustomValidator">
The CustomizableAdapter class is an abstract class that implements the Customizable interface and provides the following helper methods:
getFieldToValidate()
fieldToValidate
attribute in the <wl:validator>
tag)
getErrorMessage()
errorMessage
attribute in the <wl:validator>
tag.
getExpression()
Instead of extending the CustomizableAdapter class, you can implement the Customizable interface. For more information, see the Javadocs for weblogicx.jsp.tags.validators.Customizable.
import weblogicx.jsp.tags.validators.CustomizableAdapter;
public class myCustomValidator extends CustomizableAdapter{
public myCustomValidator(){
super();
}
public boolean validate(javax.servlet.ServletRequest req)
throws Exception {
String val = req.getParameter(getFieldToValidate());
// perform some validation logic
// if the validation is successful, return true,
// otherwise return false
if (true) {
return true;
}
return false;
}
}
This sample code shows the basic structure of a JSP that uses the WebLogic JSP form validation tags. A complete functioning code example is also available if you installed the examples with your WebLogic Server installation. Instructions for running the example are available at samples/examples/jsp/tagext/form_validation/package.html
, in your WebLogic Server installation.
<%@ taglib uri="tagl" prefix="wl" %>
<%@ taglib uri="input" prefix="input" %>
<wl:summary
name="summary "
headerText="<font color=red>Some fields have not been filled out correctly.</font>"
redirectPage="successPage.jsp"
>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<% if(summary.size() >0 ) {
out.println("<h3>" + headerText + "</h3>");
} %>
<% if (summary.size() > 0) {
out.println("<H2>Error Summary:</h2>");
for (int i=0; i < summary.size(); i++) {
out.println((String)summary.elementAt(i));
out.println("<br>");
}
} %>
<wl:form method="GET" action="successPage.jsp">
User Name: <input:text name="username"/>
<wl:validator
fieldToValidate="username"
validatorClass="weblogicx.jsp.tags.validators.RequiredFieldValidator"
errorMessage="User name is a required field!"
>
<img src=images/warning.gif> This is a required field!
</wl:validator>
<p>
Password: <input type="password" name="password">
<wl:validator
fieldToValidate="password"
validatorClass="weblogicx.jsp.tags.validators.RequiredFieldValidator"
errorMessage="Password is a required field!"
>
<img src=images/warning.gif> This is a required field!
</wl:validator>
<p>
Re-enter Password: <input type="password" name="password2">
<wl:validator
fieldToValidate="password,password2"
validatorClass="weblogicx.jsp.tags.validators.CompareValidator"
errorMessage="Passwords don't match"
>
<img src=images/warning.gif> Passwords don't match.
</wl:validator>
<p>
<input type="submit" value="Submit Form"> </p>
</wl:form>
</wl:summary>
</body>
</html>