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!
Asked
Active
Viewed 2.5k times
0
-
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 Answers
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
-
-
-
i need validation in all form of my project.so single servlets can Be used for multiple form o jsp – gaurab pradhan Jun 29 '12 at 11:24
-
In that case, you should be doing this using Filter. And apply the filter for the Jsps required. – Ramesh PVK Jun 29 '12 at 11:28
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
-
-
0
You can do client side input validation. I suggest to use JQUery plugin for validation.

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