jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Moving a bean from request to session context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Moving a bean from request to session context" Watch "Moving a bean from request to session context" New topic

Moving a bean from request to session context

Buzz Andersen
Ranch Hand

Joined: Aug 28, 2001
Posts: 54
I am using JavaBeans to handle HTML form submissions, and have things set up so that an initial form submits to a validation page, which creates a JavaBean in request scope from the parameters in the request. This page uses the bean to do some validation, and then does one of two things:
1. If everything is valid it forwards to a confirmation page, which uses the information in the bean to reprint the entered values (this page has access to the bean because it is forwarded).
2. If there is a problem, it uses the bean to print out errors and redisplay the form, with all previously entered values intact.
Here is a picture of the flow:
Now my problem: I want to put something on the confirmation page that says "Is this correct?" and allow the user to go back to the initial form page if it is not. However, I would like this initial page to be populated with the values already entered, again using the JavaBean.
In order to accomplish this, I would like to move the bean from request scope to session scope, and then have the initial page look for it there. If possible, I would like to have the initial page simply have a useBean tag that locates the bean.
So, after all this, my question: is there a way to manually put a bean in session scope so that a useBean tag can find it?
Buzz Andersen
Buzz Andersen
Ranch Hand

Joined: Aug 28, 2001
Posts: 54
Alright, a little experimentation has led me to just adopt a practice of putting the bean in session scope across the board, rather than trying to move the bean over from request scope. Now I have another question:
When the bean gets updated by the "jsp:setProperty name='x' value='*'" in validate.jsp, the fields only get updated if there is something in them. If, for example, I delete the text completely from a text field and then resubmit, the setter method for that property is not called and the old value remains. I can change the text in the field--as long as there is something there the setter method gets called. But if the field is blank, it keeps its old value.
Does anyone know of a good way around this problem? I thought about writing a Javascript that submits "null" values or something if it finds that form fields are blank, but I'd rather not do it that way...
Perhaps this behavior is simply a good reason to do things the way I was originally planning (i.e. doing everything in request context and only moving to session context when necessary).
Thanks again...
It is sorta covered in the JavaRanch Style Guide.
subject: Moving a bean from request to session context
Similar Threads
Validation Best Practice
JSP:Include question
jsp:useBean type requirements
setProperty and thread safety
Sharing variables between jsp:include files