In the above applications, I'm using one servlet which would determine the process based on the RequestURI.
When I tried to display the request object, I could see the same request object being displayed . But however, the attributes set before forwarding the request using RequestDispatcher is found to be lost after forward. The attribute "color" is displayed as null second time.
I believe that RequestDispatcher would not create any new request object before forwarding to a new resource. Assuming that if RequestDispatcher is creating a new request object, then I should be seeing two different display statements. But I'm seeing the same request object being displayed. Then why the attribute "color" set initially is lost when I tried to display it again after forward.
I know that its a good idea to store attributes in session rather than request, but how the attributes are lost when the initial request object is not lost.
Please clarify on this.
Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Whatever attributes you set in a request is lost after the response is sent to the client. That's why they are called "request" attributes - they are valid only for the duration of the request. If you want to set attributes that are longer-lived, use session attributes or context attributes.
Ping & DNS - updated with new look and Ping home screen widget
Joined: Oct 20, 2007
Thank you Ulf,
But in my code I used RequestDispatcher to transfer the request object to new resource. But here how come client(Browser) is receiving the request. I'm sorry I might be confused here but would be glad to know concept behind.
I completely go with you and accept that in order to keep cart sharable it is not a good idea to instantiate it in init() method. But my idea is not to consider any concurrent users at this point. I'm just worrying why the request object attributes which was set before are not carried forward after request dispatcher forward method. I'm not able to understand the relation between lost of request attributes because of instantiating the cart in init(). Please excuse me if I'm missing any link here.
I'm not sure where your request attributes are being lost. Can you explain, in steps what you're doing (from the web user's perspective) and tell me when you notice them not there?
Joined: Oct 20, 2007
Please find the steps below :
1) User launches the login page and enters his credentials. 2) The credentials go to the servlet and after authentication, it launches other view, where it shows a list of few books select. Here serlvet stores the user name as an attribute in request object. 3) on the book selection page, user would select books, by selecting checkboxes and sends the request back to the same servlet. Note : Servlet has a mechanism to determine where the request has come from and determines the next action and view accordingly. Servlet adds all the selected books as objects in ShoppingCart(it has an ArrayList) and adds the cart object as another request attribute and forwards the reques to result page. 4) Here in the result page, it should display the user name and selected books.
Problem : On the results page, the user is displayed as null, but the books in cart are displayed properly.
... as another request attribute and forwards the reques to result page.
You do realize that request scoped variables are only valid for one request, right?
If the user logs into one screen all the request attributes will be gone once the second screen is displayed in the browser. Maybe you have request and session confused?
Joined: Oct 20, 2007
Huh I missed the point here . You are right Ben and Bear, request scoped variables would not exist across different requests. Probably I was confused as the same request object is being used for the entire transaction.
So even though the same request object is being used for different dispatcher forwards, the attributes set would not exist across different requests. We need to use session scoped variables for this..