0

I am new to JSP and servlet, could you help me for form validation like to check weather text field is empty or not, in an easy way. Thank you!

Vy Do
  • 46,709
  • 59
  • 215
  • 313
gaurab pradhan
  • 21
  • 1
  • 2
  • 5
  • JSP is nothing but a part of HTML. you can put validations in – Alpesh Prajapati Jun 29 '12 at 09:10
  • Read book: "Java Servlet & JSP Cookbook", page 274, using JavaScript to Validate Form Values in a Servlet (validate at server-side) (You can read this section in Google books free: https://books.google.com.vn/books?id=j8wSW13lHh4C) – Vy Do Jan 09 '16 at 05:10

3 Answers3

2

You can do the validation both at client side and server side. You can use either JavaScript or JQuery to validate at client side.

Here are the references for them:

Here is the example which explains how to handle form post data at the server side using Servlet:

<form action="formpost" method="post">
<input type="text" name="name" />
<input type="text" name="age" />
<input type="submit" />
</form>

Write the servlet reads the post parameters and validates them:

public class FormPost extends HttpServlet
{
 protected void service(HttpServletRequest request, 
   HttpServletResponse responst) throws ServletException, IOException
 {

   if(request.getParameter("name") != null){
      String nameParameter = request.getParameter("name");
   } else {
       //write you validation code here
   }

   int ageParameter = Integer.parseInt(request.getParameter("age"));



 }

}
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

If purely using JSP and Servlet, i recommend springing off from JavaScript abstraction libraries like JQuery for this task.

Such is the bassistance plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • in terms of validation, both client side validate and server side validate should be considered, so besides the Jquery to perform the client side validation, it is also necessary to do a double check for some important fields at server side as well. – zinking Jun 29 '12 at 09:15
  • Hi zinking. Question asked for "form validation". – Oh Chin Boon Jun 29 '12 at 09:17
  • well, my take for that is he is asking about general "validation" – zinking Jun 29 '12 at 09:18
0

You can do client side input validation. I suggest to use JQUery plugin for validation.

JR Galia
  • 17,229
  • 19
  • 92
  • 144