Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Turn HTML Form Input Into Java Objects

JavaServer Pages can make Web Application development a lot simpler. If you identify the most common kinds of data entered into forms, you can quickly start to build a library of classes that you can reuse to store form data.


advertisement
avaServer Pages can make Web Application development a lot simpler. When coupled with JavaBeans, turning HTML form input into Java objects is a lot faster to implement. If you identify the most common kinds of data entered into forms, you can quickly start to build a library of classes that you can reuse to store form data. For example, any kind of e-business applications will usually require name, address, and other contact information.

Often you will have to validate those input items as well. A simple approach to handling this is to create JavaBeans that only store inputs, and a set of validating classes that take care of verifying the validity of data according to the appropriate business rules. For example, you may have a Login class that stores user sign-on information and a LoginValidator class that makes sure the user has an account on the system and checks the password against the user password database.

The following example demonstrates the basic concept. It shows a NewLogin bean that is used to store a username, password, and verification password:



public class NewLogin implements java.io.Serializable { protected String _userName_, _password_; protected String _verificationPassword_; public NewLogin() { this(null, null, null); } public NewLogin(String userName, String password, String verify) { _userName_ = userName; _password_ = password; _verificationPassword_ = verify; } public void setUserName(String user) { _userName_ = user; } public void setPassword(String password) { _password_ = password; } public String getUserName() { return _userName_; } public String getPassword() { return _password_; } public void setVerificationPassword(String verify) { _verificationPassword_ = verify; } public String getVerificationPassword() { return _verificationPassword_; } }

Then there's the NewLoginValidator class that just checks that the two passwords are the same. It should also check that the username is not already in use, but that's an application-specific detail.

public class NewLoginValidator { public void validate(Object login) throws ValidationException, ClassCastException { NewLogin newLogin = (NewLogin)login; String verify, password; password = newLogin.getPassword(); verify = newLogin.getVerificationPassword(); if(verify == null || password == null) throw new ValidationException( "Both passwords were not entered.", newLogin); if(!verify.equals(password)) throw new ValidationException( "Password and verification password do not match.", newLogin); } }

Finally, here are examples of an html form:
Username:


Password:

Verification Password:


Here's an example of a JSP page:

<%@ page import="examples.*" %> <jsp:useBean id="newLogin" scope="page" class="examples.NewLogin"/> <jsp:setProperty name="newLogin" property="*"/> <% NewLoginValidator validator = new NewLoginValidator(); try { validator.validate(newLogin); } catch(ValidationException e) { out.println("

EXCEPTION:\n" + e.getMessage() + "

"); return; } %> <html> <body> Account for <%= newLogin.getUserName() %> created. </body> </html>




   
Daniel F. Savarese holds a B.S. in astronomy and an M.S. in computer science, both from the University of Maryland, College Park. He is the author of the OROMatcher regular expression library for Java. Reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date