This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Reatining Form Values After an exception is thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reatining Form Values After an exception is thrown" Watch "Reatining Form Values After an exception is thrown" New topic
Author

Reatining Form Values After an exception is thrown

Albin Jose
Greenhorn

Joined: Feb 11, 2008
Posts: 11
Hi,
I have a html form where in i get values from the user, i m sending it to a servlet for validating the values entered by the user,The servlet in turn calls a EJB which interacts with the database via a procedure calls,Now my problem is if the database procedure throws some exception , it is rethrown to the servlet via the EJB and the servelt redirects it to the same html page(where the user had entered earlier).Now i have to retain all the values previously entered by the User(i Know i have to keep it in session )but let me know the better methods of where to put them in session like which place these variables have to be set
Albin Jose
Greenhorn

Joined: Feb 11, 2008
Posts: 11
wanted to add one more, are there any other methods other than setting it in session
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41054
    
  43
You don't need a session for this. You can catch the exception in the servlet, and then forward the request to the JSP page that generates the HTML form. Just set a request attribute that tells the JSP page that there was an error, so that it knows to repopulate the form fields with the request parameters. Most web frameworks (like Struts and Stripes) can do this for you automatically.


Ping & DNS - my free Android networking tools app
Albin Jose
Greenhorn

Joined: Feb 11, 2008
Posts: 11
Thanks ,
But i don use jsp, i m using the same java file(servlet) to render my html page,i use place holders for displaying back to the user, but i need to retain the values to be posted back.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41054
    
  43
That's even easier then. If the code to create the HTML is part of the servlet, then you have access to the request object that contains all the data the user entered. So you can create an HTML page that contains those values prepopulated.

As an aside, you should seriously consider to use a JSP page instead. It's much easier to maintain, and there's no need to reload the web app upon each code change.
 
Don't get me started about those stupid light bulbs.
 
subject: Reatining Form Values After an exception is thrown
 
Similar Threads
check boxes and servlets
Dynamically populating drop down menu with database values
Form Bean - Session Scope
form contents
Moving a bean from request to session context