Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Servlets and the fly likes setAttribute and getAttribute()  with Context Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setAttribute and getAttribute()  with Context Object?" Watch "setAttribute and getAttribute()  with Context Object?" New topic

setAttribute and getAttribute() with Context Object?

darine darine
Ranch Hand

Joined: Mar 08, 2001
Posts: 43
Hi all
i have two servlet in the same app,i stored an attribute in the context object using setAttribute and i tried to retrieve it from the second servlet using getAttribute,i got null,even i casted it.methhods as the following:
servlet1Context.setAttribute("name","this a test");
String str1=(String)servlet2Context.getAttribute("name");
thanks for the help.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
You setAttribute in servlet1Context and getAttribute in servlet2Context. What if you getServletContext of servlet1 where you stored the attribute?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Listen it works perfectly fine i have set it in one servlet and
got it in second Servlet.Following is the code in init method.
ServletContext servletContext = null;
DatabaseConnection db = null; //DatabaseConnection is my
defined class.
servletContext = config.getServletContext();
db = servletContext.getAttribute("db")
it works perfectly fine.
Pls check your code again.In first servlet only try to get the object from context after setting it.See what do u get..??
I hope abv will help.

I agree. Here's the link:
subject: setAttribute and getAttribute() with Context Object?
It's not a secret anymore!