0

I'm programming a car-hire web-site as my university project. There are some restrictions like * no JavaScript*, i can only use .jsp and servlets.

I've got few forms, i.e. booking form for customers, form to edit vehicles fleet for manager, etc. Lets say that data entered in the form needs to be validated and saved to a database. So i was thinking of checking weather the data is valid with the help of a servlet.If some field/fields were filled incorrectly i want (with the help of my servlet) to reload the page with form and to ask to re-enter data.

Question. How do I reload a page with a form on it with a help of servlet suggesting that some changes to this page should be made (highlighting of problematic fields, label stating that smth went wrong, etc.)?

Thanks for considering my question.

Denys
  • 4,287
  • 8
  • 50
  • 80
  • http://stackoverflow.com/questions/2292260/can-the-same-servlet-perform-input-validation-from-an-html-form-it-displayed - relevant topic – Denys Dec 05 '11 at 21:29

1 Answers1

0

This is a basic functionality of every MVC framework (Stripes, Spring MVC, Struts, etc.). The idea is to have a servlet prepare a bean with default (usually empty) values, store this bean in a request attribute, and then dispatch to a JSP which displays a form populated with the values stored in the bean.

When the form is submitted, another instance of the same bean is populated with the request parameters, then validated, and if an error is found, the servlet stores the bean in the same request attribute as before, and also error messages in another request attribute. It then redispatches to the same JSP. This JSP redisplays the same form, and thus redisplays the value that the user has just submitted. It also displays the errors stored in the request attribute (and which was null when the JSP was executed for the first time).

Each error message can be associated with a form field name, so that the JSP can, for example, associate an "error" CSS class to a field if an error exists for this field.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255