Server side Validation
Posted on: February 14, 2011 at 12:00 AM
In this tutorial you will learn how to validate data on server coming from client

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,

Write a simple login.jsp file as


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:head />
<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>


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

Write a Simple Model for login application

package net.roseindia.model;

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";	


Then Write an Action class for application

package net.roseindia.action;

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

import net.roseindia.model.LoginModel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.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();

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

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

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

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

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.


<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"

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

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


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

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


<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<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.roseindia.model.LoginModel"
<result name="input">resources/registration.jsp</result>

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



When you run this application it will display message as shown below:


Download Select Source Code

Related Tags for Server side Validation :