Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

How to validate HTML form using servlet

 
Kasparov Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello All,

I would like to know, how can I validate my HTML form in servlet? My same servlet performing some calulation if form is validated. Can anyone help me?

Thanking You,
Kasparov
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
What specifically is your problem? Your question is vert vague and open-ended.
 
Kasparov Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello Bear,

Thanks for your reply. I have one html form. on action I am calling servlet page. That servlet page has certain calculation as per user input on html page. I want to validate all those user input before it go to calculation code. If there is missing value or something wrong input, then it should go back to pointing that filed. I am not getting how to do?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Report post to moderator
Take a look at the signatures of the servlet method you're using (probably doGet() or doPost()). You will see that it has parameters HttpServletRequest and HttpServletResponse. Through the HttpServletRequest you can access the values from your form, by using the getParameter() method: if a field in your form has the name "foo", you would call request.getParameter("foo") in the servlet to access it. Note that what you get from the method is a string, which you have to transform to the correct type. Then you can simply check if it has the expected value.

If the value is wrong, you forward back to the form page, by using the request.getRequestDispatcher().forward() method. You should also put an error message in the request with request.setAttribute(), to display on the form page so the user knows what's wrong.
 
Kasparov Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello D. Ogranos,

Thanks for your really very good explanation.
 
Kasparov Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Report post to moderator
How can I send alert message from servlet to html file?
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
to do validation on server side you have access to HttpRequest object and can use getParameter(String str)

another way is to check user input inside html(better option)
as your html page is accepting the user input, you can make your html page to do validation using the javascript.

 
Tim Moores
Bartender
Posts: 2745
38
  • Mark post as helpful
  • send pies
  • Report post to moderator
naveen yadav wrote:as your html page is accepting the user input, you can make your html page to do validation itself before request is send to servlet. you can do this by using the javascript inside your html.

That's insufficient; all validations done in JavaScript also need to be done on the server.

See my reply in the other topic where you asked this question; you really should not post the same question multiple times.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing this topic so that any further discussion can occur in the newly opened topic.
 
    Bookmark Topic Watch Topic
  • New Topic