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 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.*" %><%  NewLoginValidator validator = new NewLoginValidator();  try {    validator.validate(newLogin);  } catch(ValidationException e) {    out.println("
EXCEPTION:
" + e.getMessage() + "

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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may