Creating validation class

Tuesday, March 2, 2010 Posted by Unknown
Up to now we created
insertupdate.jsp
ControllerServlet.java
package model and bean class
now in this post we will create a package and a class
crate a package validation and with in this package create a class ValidateForm.

package validation;

/**
*
* @author Jagadeesh
*/
public class ValidateForm {


}

now go to servlet. in that servlet declare variables to get form values and create a method for setting values to bean
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserBean;
import validation.ValidateForm;

/**
*
* @author Jagadeesh
*/
public class ControllerServlet extends HttpServlet {

//declare values to get form values from jsp page
String userName;
String dateOfBirth;
String email;
String phoneNo;
String action;

UserBean bean = new UserBean();
ValidateForm validateform = new ValidateForm();

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//get the values from jsp page
userName = request.getParameter("userName");
dateOfBirth = request.getParameter("dateOfBirth");
email = request.getParameter("email");
phoneNo = request.getParameter("phoneNo");
action = request.getParameter("action");
if(action.equals("submit"))
{
//set values to bean.For this call below method
setValuesToBean();


}
}
catch(Exception e)
{
out.println(e);
}
finally {
out.close();
}
}
//this method is used to setvalues to bean
public void setValuesToBean()
{
bean.setUserName(userName);
bean.setDateOfBirth(dateOfBirth);
bean.setEmail(email);
bean.setPhoneNo(phoneNo);

}


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


public String getServletInfo() {
return "Short description";
}

}



in next post we will learn how to validate form values and how to send errors to jsp page
Labels:
  1. very nice.very helpful

  2. Thank you. follow my new blog http://jagadeeshmanne.blogspot.in

Post a Comment