wood burning stoves*
The moose likes Struts and the fly likes Problem accessing a servlet context attribute from an action servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem accessing a servlet context attribute from an action servlet." Watch "Problem accessing a servlet context attribute from an action servlet." New topic
Author

Problem accessing a servlet context attribute from an action servlet.

Scott Updike
Ranch Hand

Joined: Feb 16, 2006
Posts: 92
I need to be able to access (and ultimately replace) a servlet context attribute from within an action servlet that extends the MappingDispatchAction class. I cannot seem to get the right syntax to access the attribute so I can test if I need to update/replace it. I can provide code samples if needed, but was hoping this would be an easy issue to resolve.


Thanks in advance,
Scott
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's pretty easy. Inside any method in your Action class, just code:

ServletContext servletContext = getServlet().getServletContext();

The Action class, which is an ancestor of your class has the getServlet() method that retrieves the instance of ActionServlet that called your Action class.


Merrill
Consultant, Sima Solutions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem accessing a servlet context attribute from an action servlet.
 
Similar Threads
working with drop down menus
how can you be sure abt session attribute from any other servlet in the same context
How to get servlet context or similar object in Struts
NullPointerException
Servlet Context