Turn HTML Form Input Into Java Objects

Turn HTML Form Input Into Java Objects

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

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

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

public class NewLogin implements {  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:



Verification Password:

Here’s an example of a JSP page:

<%@ page import="examples.*" %><%  NewLoginValidator validator = new NewLoginValidator();  try {    validator.validate(newLogin);  } catch(ValidationException e) {    out.println("
" + e.getMessage() + "

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


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist