• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setAttribute and getAttribute() with Context Object?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You setAttribute in servlet1Context and getAttribute in servlet2Context. What if you getServletContext of servlet1 where you stored the attribute?
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic