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 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:



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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes