FYI: be careful how you setAttribute and getAttribute for session requests.

I've read a few resources online that state that using request.getSession().getAttribute("name") is the same as using request.getAttribute("name") and this is not generally true.

My development framework uses the Jetty 5.x embedded Jetty jsp context and web server. The jsp code is sensitive to where in the object hieararchy the "get" is used, so I recommend that any where you use:

request.setAttribute("name")

to get consistent results use

request.getAttribute("name")

to recover the attribute from the session.

Similarly , if you use :

request.getSession().setAttribute("name")

use

request.getSession().getAttribute("name")

to extract the object from the session. I spent a few hours over looking an inconsistency in how I was using the methods before I realized it was the source of an intermittent null pointer exception so keep your sets and gets consistent to avoid weird exceptions!

Regards,

David

[989 byte] By [sent2nulla] at [2007-11-27 3:54:25]
# 1

>I've read a few resources online that state that using

>request.getSession().getAttribute("name") is the same as using

>request.getAttribute("name") and this is not generally true.

More than that. It is patently wrong.

Request attributes and session attributes are in completely different scopes.

request.getSession().getAttribute is maybe the same as session.getAttribute()

Because the implicit session variable comes from calling request.getSession()

To avoid confusion in my servlets, I normally get the session as a local variable anyway. It is implicit in JSPs, so I use the same name in servlet.

ie

HttpSession session = request.getSession();

session.getAttribute("name");

evnafetsa at 2007-7-12 8:58:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> to extract the object from the session. I spent a few

> hours over looking an inconsistency in how I was

> using the methods before I realized it was the source

> of an intermittent null pointer exception so keep

> your sets and gets consistent to avoid weird

> exceptions!

Of course Http Request and Http Session are 2 different objects, with different behavior.

Thanks for sharing.

> Regards,

>

> David

appy77a at 2007-7-12 8:58:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
This is not new to us :) I can imagine anyway that it is confusing for unaware developers. It's just all about scoping. Request attributes have a lifetime of one request.Session attributes have a lifetime of one session.That's all you need to know at least.
BalusCa at 2007-7-12 8:58:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> This is not new to us :)

>

> I can imagine anyway that it is confusing for unaware

> developers. It's just all about scoping.

>

> Request attributes have a lifetime of one request.

> Session attributes have a lifetime of one session.

I agree... it is far better to remember the concepts than remember the code, because things make better sense that way

> That's all you need to know at least.

appy77a at 2007-7-12 8:58:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Whats new here? i think every (most of) jsp developers knw the difference between request and session scope attributes,sudhir http://www.jyog.com
Sudhir_nimavata at 2007-7-12 8:58:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...