Advertisment

ASP .NET like Validations with Java

author-image
PCQ Bureau
New Update

Microsoft’s ASP .Net technology showcased Web forms, server side controls, handling of events generated by Web UI components at the server side, and powerful yet easy to use validation controls. JavaServer Faces targets to bring similar technology to Web server side Java, ie, JSP or Servlets. With such technologies, writing Web applications is becoming similar to writing a desktop application, with the distinction between client (browser) and the server (Web server) diminishing. 

Advertisment

Traditionally, it has always been easier to validate user input at the client side using JavaScript/VBScript, as compared to at the server side. It becomes time consuming and redundant to code forms, which will retain correct values and prompt for incorrect values with an error message, after a roundtrip to the server. 

It is here where technologies like ASP .Net and JavaServer Faces come to the rescue. Anyone who has worked with ASP .Net will appreciate its easy to use validation controls, which takes care of server side validations (as well as client side). You just need to write a single line of code as follows



 



and it is ensured that the user enters some value in the text field named field1. Let’s see how to achieve similar validation with JavaServer Faces. 

The basics

The JSP page, which needs to be validated, must contain the following two lines (taglib declaration).



<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h” %>

<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>



The prefix ‘h’ and ‘f’ are by convention. Next, you must enclose the entire form (containing the input elements) within the and . The form elements on the page are specified using a ‘h’ prefix — like (HTML Form), (textfield) and (submit button). Below is a complete code, which includes a required field validator () which checks whether some value is entered in the text field named field1. 



<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h” %>

<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>













 














 




Note the following portion of the code:







 



in which the tag for the validation control is encompassed within the tag pair of the text field which is to be validated. The tag is used to display any validation error. We place this tag right next to the text field, so that the user sees the error message (in red color) next to the field. The ‘for’ attribute of the must be set to the field name whose validation error is to be displayed. 

See it working

The easiest way to see the code working is to deploy it using JWSDK (Java Web Services Development Kit). You can find JWSDK 1.2 on the April 2003 issue of PCQuest Xtreme CD. Refer to the article A Simple Web Service in Java on page 84 in the August 2003 issue of PCQuest for instructions on how to install and use the kit. We will deploy the above code as a Web application using JWSDK. For this first we need to create the directory structure for a Web application. Create a subdirectory named test (say) in the Web apps subdirectory of JWSDK installation. Create a directory named WEB-INF under the test directory. Subsequently, create directories named classes and lib within WEB-INF. We have created a directory structure for a web application. To understand about a Web application’s directory structure, refer to the article Web Applications with Tomcat on page 75 in the November 2002 issue of
PCQuest. 

Advertisment

Next, write the above code in a file named form.jsp and save it in the test directory. Next create a file named web.xml in the Web-INF directory and type in the following lines in the file.






“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”

http://java.sun.com/dtd/web-app_2_3.dtd”>





Faces Servlet javax.faces.webapp.FacesServlet

1





Faces Servlet

/faces/*







Stop and start the JWSDK’s tomcat application server as:



catalina.bat start (Windows)

./catalina.sh start (Linux)



Assuming that you are working on the same machine with the JWSDK installation, key in the following URL in your Web browser.



http://127.0.0.1:8080/test/faces/form.jsp



Clicking on the Submit button without entering any value in the test field will show up an error message next to the text field. 

Note that though form.jsp resides in test directory, we still access it as test/faces. Accessing it in this way is required, because it invokes the servlet
(javax.faces.webapp.FacesServlet) responsible for processing the JavaServer Faces tags. In other words, any page containing JavaServer Faces tags must be invoked using the faces/ prefix in the URL. 

The other validatiors available with JavaServer Faces are validate_doublerange , validate_longrange, validate_stringrange and validate_length. The first three validators can be used to check whether the value entered falls within a specified range of decimal integer or string value. The validate_length can be used to ensure that the user does not type in more than a prescribed number of characters — useful in case of an HTML
textarea. 

Besides these standard validators one can code his/her own validators. The JavaServer Faces validators do not perform client side validations like ASP.

Advertisment

Net. But note that the final of version JavaServer Faces technology is not yet released and what we are working with is EA 4 (Early Access). With final version we may expect a couple of more standard and feature rich validators and still other validators from third party vendors. 

For further information, documentation and tutorials on JavaServer Faces, refer to the URL http://
java.sun.com/j2ee/javaserverfaces/

Shekhar Govindarajan

Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: