• Post Reply 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 all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to restore form Data in Java  RSS feed

Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!