Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to validate HTML form using servlet

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
How can I send alert message from servlet to html file?
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing this topic so that any further discussion can occur in the newly opened topic.
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic