• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Lost Request Attributes

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cart.setCart(a);

Where are you creating your cart object?
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, cart is an instance variable in your servlet?
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... 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
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic