GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to validate HTML form using servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

How to validate HTML form using servlet

Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

What specifically is your problem? Your question is vert vague and open-ended.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
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

Joined: Feb 02, 2009
Posts: 214
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

Joined: Jan 23, 2012
Posts: 40
Hello D. Ogranos,

Thanks for your really very good explanation.
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
How can I send alert message from servlet to html file?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

Closing this topic so that any further discussion can occur in the newly opened topic.
 
GeeCON Prague 2014
 
subject: How to validate HTML form using servlet