GeeCON Prague 2014*
The moose likes Servlets and the fly likes Lost Request Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Lost Request Attributes" Watch "Lost Request Attributes" New topic
Author

Lost Request Attributes

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
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 - my free Android networking tools app
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
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.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

cart.setCart(a);

Where are you creating your cart object?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

So, cart is an instance variable in your servlet?
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61316
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61316
    
  66

Please show us how you are setting up the user scoped variable in the servlet, and how you are accessing it in the JSP.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

... 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?
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
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..
 
GeeCON Prague 2014
 
subject: Lost Request Attributes