Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setAttribute and getAttribute() with Context Object?

 
darine darine
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You setAttribute in servlet1Context and getAttribute in servlet2Context. What if you getServletContext of servlet1 where you stored the attribute?
 
james edwin
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic