Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets

 
arunas rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the application object(servlet context) is accessed from any where
in the web application? what is the role of application.getAttribute(" ") method in this?
 
Johnson Abraham
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Application container can be accessed in this fashion

javax.servlet.ServletContext context = getServletContext();

now context is your application object.

You can use the following line of code to store any object at the application level:

context.setAttribute("myObject",myApplicationObject);

Now if you want to retrieve the object from the application scope you can use the following line of code:

MyApplicationObject myApplicationObject = (MyApplicationObject) context.getAttribute("myObject");

Johnson
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic