This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "setAttribute and getAttribute()  with Context Object?" Watch "setAttribute and getAttribute()  with Context Object?" New topic
Author

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
Sheriff

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

JavaBeginnersFaq
"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
Hi,
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.


Regards,
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setAttribute and getAttribute() with Context Object?