skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 19, 2012

Server side validation in JAVA

Posted by Raju Gupta at 3:00 PM – 0 comments
 

This Program helps beginner to easily understand the server side validation in java.

This is a simple generic code of validation which can be modified and can be used to understand complex server side validation examples.

It is very good code for the beginner to understand server side validation.

Server Side Validation :
It is very important to validate the data coming from the client side, so that wrong data could not process into the application.
In Struts you can validate the data as follows,



/*1. Write a simple login.jsp file as
login.jsp :*/

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:head />
<center>
<h1>Please Login</h1>
<s:form action="register">
<s:textfield name="userName" key="userName" label="User Name"></s:textfield>
<s:textfield name="password" key="password" label="Password"></s:textfield>
<s:submit value="Register"></s:submit>
</s:form></center>

/***********************2. home.jsp :*************************/

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Home Page</title>
</head>
<body bgcolor="lightblue"><br><br><br><br><br>
<h1>Welcome <s:property value="userName"/></h1>
</body>
</html>

/************3. Write a Simple Model for login application************/

//LoginModel.java :

package net.mumbaiilp.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class LoginModel implements Serializable {

 private static final long serialVersionUID = 1L;

 private String userName;
 private String password;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
 
 public String displayForm(){
 return "input"; 
 }

}

/*************4. Then Write an Action class for application******************/

//LoginValidation.java

package net.mumbaiilp.action;

import java.util.ArrayList;
import java.util.List;

import net.mumbaiilp.model.LoginModel;

import com.xyztcs.xwork2.ActionSupport;
import com.xyztcs.xwork2.ModelDriven;

public class LoginValidation extends ActionSupport implements
  ModelDriven<LoginModel> {

 private static final long serialVersionUID = 1L;
 LoginModel model;
 List courseList = new ArrayList();
 List gender = new ArrayList();

 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  if (model.getUserName() != "") {
   return SUCCESS;
  } else {
   return INPUT;
  }
 }

 @Override
 public LoginModel getModel() {
  // TODO Auto-generated method stub
  model = new LoginModel();
  return model;
 }

 @Override
 public void validate() {
  // TODO Auto-generated method stub
  if ("".equals(model.getUserName())) {
   addFieldError("studentName", getText("userName"));
  }

  if ("".equals(model.getPassword())) {
   addFieldError("roll", getText("password"));
  }
  super.validate();
 }
}

/*5. After that write a xml file for server side validation. The naming convension for writing this aml file is it should start with the class class name for which it is being 

writtem-validation.xml. Example for Login Action class it should be Login-validation.xml.

LoginValidation-validation.xml :*/

<!DOCTYPE validators PUBLIC
"-//TCSMumbaiILP Group//XWork Validator 1.0.2//EN"
"http://www.xyztcs.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="userName">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>

<field name="password">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>

</validators>

/******6. Then Write a properties file for generating error message on the page.*****/ 

//LoginValidation.properties :

userName = Please Enter Name
password = Please Specify Roll No
requiredstring = ${getText(fieldName)} is required

/******************************7. struts.xml******************************/

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />

<package name="roseindia" namespace="/" extends="struts-default">

<action name="registrationForm" class="net.mumbaiilp.model.LoginModel"
method="displayForm">
<result name="input">resources/registration.jsp</result>
</action>

<action name="register" class="net.mumbaiilp.action.LoginValidation">
<result name="success">resources/home.jsp</result>
<result name="input">resources/registration.jsp</result>
</action>

</package>

</struts>

Now, you can run the application and check how its working.

Labels: Validation Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger