Two Laptop Bag
The moose likes Java in General and the fly likes How to restore form Data in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to restore form Data in Java" Watch "How to restore form Data in Java" New topic

How to restore form Data in Java

Saurabh KumarD

Joined: Jul 23, 2009
Posts: 12
I am working with a registration form.
Currently when I press submit button and if any input is wrong , I am redirected to same page with all the previously filled fields blank. I want that my previously filled value persist.

Can any one help me out , how to achieve this is Java. I am using Java, .page file and xsl for rendering things. I can't use PHP for this.
Either I can do this on my xsl file or on Java script.

Please help. I do urgently need this.

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
If you want to store form data you typically put it into a data object in the session (after submitting). If you go back to your output page, you access that object and put the values in there into the form fields. Best do a search in the jsp and servlet forums here on this site, this question probably comes up frequently.
Kevin McMahon

Joined: Nov 03, 2008
Posts: 19
If this is just a single jsp page form (i.e the form itself doesn't span multiple pages) you don't need to use the session.

Maybe this MVC example will help? This is just a basic example. There are many different methods to do the same thing.

(M) = Model
(V) = View
(C) = Controller

First Part
1) User directs browser to RegistrationFormController (C) url
2) LoginFormController is a servlet that creates an empty RegistrationForm bean and puts it on the request object, named "regDetails". The bean has methods such as setFirstName(String name) and getFirstName().
3) LoginFormController forwards (dispatches) the request to registrationForm.jsp (V)
4) registrationForm.jsp uses JSTL to put values from bean (empty at the moment) in to the form values.

Second Part
1) User fills in form and clicks submit button. This posts the data to the RegistrationController (C)
2) RegistrationController takes the submitted values from the request object and creates a RegistrationForm bean using these values.
3) RegistrationController then validates the bean values to make sure they are of the correct data types, all mandatory fields have values etc.
4) If no problems, calls some business logic (M) to do whatever you need to do with the data (save it to a database maybe?) - Note - this could also provide validation errors for example if the username already exists in the database etc.
5) If problems, forward to success.jsp to display a success message.
6) If problems, put bean back in response object. Add error message to response object. Forward to registrationForm.jsp again.

That should at least get you started...

I agree. Here's the link:
subject: How to restore form Data in Java
It's not a secret anymore!