File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to remember HTML Form fields with Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to remember HTML Form fields with Servlets" Watch "How to remember HTML Form fields with Servlets" New topic

How to remember HTML Form fields with Servlets

Paul Doherty

Joined: Jan 11, 2001
Posts: 11
I have a servlet that reads in the values from a HTML page and populates a database. The database has certain properties such as unique fields, etc. Normal procedure involves entering data into the various fields in the HTML page and then pressing the Submit button. The servlet reads in the values from the page and then tries to update the database. Usually this is fine. However if the user enters a name that is already in the DB then an Exception is thrown. The user is then presented with the page again, but has to fill in all the details again. Is there any simple way to help the page to remember the users input.
Thanks in advance,
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
Interesting. I think I will need the same functionality soon. There are probably many ways to do it, but I think I might try just using a servlet (not a .html page).
The servlet could either hold the data or get it from a bean. The first time around, the servlet prints out the .html page. You could imagine some code where the value= attribute is set to the value of the java variable. If the dbms rejects the operation, shoot out the .html again, but this time the java variables hold the data that the user typed in.
I've also seen web sites that just say 'You entered something wrong' or whatever and tells them to hit the back button (or javascript that let's them click on a link to go back). Of course, this is cheezy.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Make the .html form as a .jsp page. When user first time types in all the form data and invokes a servlet, let the sevlet call a WorkerBean by sending the HttpRequest object to one of its validation methods and let the WorkerBean validate the form and return the errors in a Vactor. The control servlet checks the returned error vector size. If it is >1 , means the user misses soem fields/typed invalid data etc. Now servlet redirects the response to same form.jsp page but this time it sends the WorkerBeans object also in the form of request.setAttribute("name",value"). and redirects to form.jsp.
What this form.jsp does is it always grabs the dynamic form fields data from the workerBean object set in HttpRequest object by the servlet. When first time user calls this .jsp file, all the form fields will have empty values because the workerbean doesnot have the forms fields as its member values. Next time when user missed some fields and the servlet redirects to this .jsp with workerbean in request scope, the pre-entered values will be displayed with appropriate error messages.
Please visit login as RM with password maha and try to click on 'List Deputations'. You can see simillar messages in other forms also.
I try to cut and paste the relavent code soon.
maha anna
I agree. Here's the link:
subject: How to remember HTML Form fields with Servlets
It's not a secret anymore!