Discussions

Web tier: servlets, JSP, Web frameworks: request.setAttribute versus session.setAttribute

  1. request.setAttribute versus session.setAttribute (5 messages)

    I want to confirm the differences between request.setAttribute(key, value) versus session.setAttribute(key, value).

    My understanding is that request.setAttribute only make the key available in the following jsp page. But session.setAttribute will make the key available in many pages, as long as in the same session, and browser not close.

    For example, the flow like page1->page2->page3->page4. session.setAttribute will make the key available in all pages. But if we use request.setAttribute in page2, then only page3 can get the key value set in page2.

    Please advise if i miss something.
    thanks!!
  2. hi Matt,

    Your understanding of the difference between request.setAttribute and session.setAttribute is perfect....

    But just to add to that
    request.setAttribute():
    >>>>>May help you in getting rid of the hidden fields.
    >>>>>Use it most of times when you just need the attribute just for the consecutive page.

    session.setAttribute():
    >>>>>May help you in keeping the information through out the session e.g. db connections
    >>>>>but be clear where to use it.

    Best of Luck.....
  3. Dear Amit, Is it possible to use request.setAttribute on a JSP page and then on HTML Submit get the same request attribute in the Servlet? I am just curious how we can do away with the hidden fields, if all we want is to just pass the contents of a huge List containing different objects, without using session. regards Vijay
  4. My understanding is that request.setAttribute only make the >>key available in the following jsp page.

    You should also consider how you move from page to page, if you use forward, then you are passing the request and could pass the request along to as many pages as you like. The key will be available as long as the request it was set in is the current request.
  5. Is it very simple to understand.

    If you know about the "page scope", you know the "request scope".

    request's attirbute is only availible in the "request" object lifetime.
    filter, servlet, jsp, include, forward are using same request object.

    page's attirbute is only availible in the "page" object lifetime.
    You can't use page's attribute in filter, because there is no page loaded yet.
    you can use them in the jsp file/servlet, but if you do a forward/include, the attribute in "page" will not pass to them.

    >request.setAttribute make the key available in the following jsp page

    NO, unless your "following jsp page" is meaning forward the page by <jsp:forward>
  6. HttpRequest object is created and maintained for each request/response call. Onse response is sent to the client, request object is destroyed. Forwarding from one page to another is done on the server, so the request is preserved. Redirecting is done using roundrip through browser, so old request is destroyed and a new one is created.