Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sever Side form Validations  RSS feed

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to validate the form on serverside.I don't want to use Java Script for form validations because of certain constraints.
My forms contain different fieds like FirstName,LastName,emailid,zipcode,some numeric fields etc.
Can u suggest me some resource from where i can get these validations in form of Java classes or in the form of methods etc.
I need complete foolproof validations.
Thanks a lot.
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,I did a little example for you. I wnated to experiment with servlets since I'm new and I built a form with 12 input fiels of differents type e.g. text, select and radio. I have an html file which calls a servlet to process. I could not put the html code here just in case it gets rendered but if you want it I could send it to you somehow. Let me know. Here is the servlet code:
* Author: ErnieBoy
* Created: Friday, 28 September, 2001 12:52:48
* Modified: Friday, 28 September, 2001 12:52:48
import javax.servlet.*;
import javax.servlet.http.*;
public class ProcessGuestBookServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
} // end init()

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String errMsg = "";
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String gender = request.getParameter("gender");
String occupation = request.getParameter("occupation");
String company = request.getParameter("company");
String emailAddress = request.getParameter("emailAddress");
String websiteAddress = request.getParameter("websiteAddress");
String homeAddress = request.getParameter("homeAddress");
String postCode = request.getParameter("postCode");
String city = request.getParameter("city");
String country = request.getParameter("country");
String comment = request.getParameter("comment");

// We will use this one to check if the e-mail address does nopt start with @
int emailAddressStartsWithAt = emailAddress.indexOf('@');
// Now let's do some validation
if(name.length() == 0) {
errMsg += "<br>Name has not been supplied!<br>";
} else if(surname.length() == 0) {
errMsg += "Surname not supplied!<br>";
} else if(gender == null) {
errMsg += "You did not tell us what gender you are!<br>";
} else if(country.equals("none")) {
errMsg += "Please choose your country from the drop down list<br>";
} else if(emailAddress.length() == 0) {
errMsg += "E-mail Address was not provided";
} else if(emailAddress.indexOf('@') <= -1) {
errMsg += "Invalid e-mail address - @ is not present";
} else if(emailAddressStartsWithAt == 0) {
errMsg += "<br>Invalid e-mail Address! It starts with @!!<br>";
} else {
// Process the data. Here you can insert into a db or whatever!
} // end validation
PrintWriter out = response.getWriter();
out.println("<title> Process GuestBook Servlet </title>");
out.println("<head><body topmargin=0 leftmargin=0>");
if(errMsg.length() != 0) {
out.println("Sorry! there were errors. Error message is " + errMsg);
} else {
out.println("Congratulations! No errors were found!");
} // end if
} // end doPost()
} // end ProcessGuestBookServlet

[This message has been edited by ernest fakudze (edited September 28, 2001).]
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to it,
you must take care to avoid NullPointerException
You can valiedate the fields as
if(name != null && name.length() == 0) {
errMsg += "<br>Name has not been supplied!<br>";
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can avoid all this messy code by using a custom tag library.
Take a look at the dotJ custom tag library. They have a set of form tags that do client-side AND/OR server-side validation. Very nice. Check the form tag samples out and compare the JSP to the code listed in the above reply. You'll see what I mean.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!