Forums Register Login

Lost Request Attributes

+Pie Number of slices to send: Send
Hi

Please find the code below :


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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
you are getting request dispatcher through ServletContext.If you will take it through request object,i think(but not sure) your problem can be solved.
+Pie Number of slices to send: Send
It does not matter how you get the RequestDispatcher.

Are you missing your attribute when you arrive on your jsp or when you come back to this Servlet?

It would help if you posted your jsp code.
+Pie Number of slices to send: Send
Hi ,

I lost my request attribute both on my servlet and my JSP.

Let me post my entire servlet and Jsp code below:

Servlet Code


JSP Code below
+Pie Number of slices to send: Send
cart.setCart(a);

Where are you creating your cart object?
+Pie Number of slices to send: Send
Sorry Ben,

I didn't post the code creating the cart object. I have created the cart object in init() method of servlet and that part is working fine as I could see its contents being displayed in the servlet.
+Pie Number of slices to send: Send
So, cart is an instance variable in your servlet?
+Pie Number of slices to send: Send
Hi Ben,

I didn't create the cart as a declarative. So Would there be any impact by initiating an object in init() method ? So you mean to say that is the root cause for the problem.
+Pie Number of slices to send: Send
 

Originally posted by Schandha Ravi:
So Would there be any impact by initiating an object in init() method ?


Think about this for a moment. You instantiate a cart in init() and store it somewhere (where? in an instance variable?).

How do share this single cart with all the people visiting your site?
+Pie Number of slices to send: Send
Hi Ben,

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Hi Ben,

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.
+Pie Number of slices to send: Send
Please show us how you are setting up the user scoped variable in the servlet, and how you are accessing it in the JSP.
+Pie Number of slices to send: Send
 

... 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?
+Pie Number of slices to send: Send
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..
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5538 times.
Similar Threads
JSP to Jsp integration
*****ServletRequest attributes doubt*******
Form Variables
getting servlet name from request?
Study kit-error attributes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:18:04.