This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 41599
    
  55
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: 41599
    
  55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reatining Form Values After an exception is thrown