Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-populate form fields on a jsp page after failed server validation

I have a very simple Java MVC web application and am using a servlet to handle form validation. If the form is validated, the request is forwarded to the appropriate view. However, if the form fails validation, the request is forwarded back to the form, which then displays the appropriate error message(s).

My question is this -- what is the most efficient way to re-populate all of the form fields with the data that was originally entered in the form by the user?

I am not using an MVC framework, just simple HttpServlets as the controller with .jsp as the view.

like image 658
elpisu Avatar asked May 11 '11 14:05

elpisu


2 Answers

The easiest and probably least effort is to just use

<input name="foo" type="text" value="${param.foo}"/>

This should default to "" when the user first visits the form.

A little more can be done to create a custom tag which binds to the request. However this is probably not the solution you were looking for.

Edit: You may want to use <c:out value="${param.foo}"/> to protect against XSS attack.

like image 67
Amir Raminfar Avatar answered Oct 27 '22 00:10

Amir Raminfar


Pass the fields back to the jsp as part of the request object. request.setAttribute(..)

Use those attributes to set the form fields.

like image 43
Kal Avatar answered Oct 27 '22 00:10

Kal