File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
jQuery in Action, 2nd edition
 
subject: Problem accessing a servlet context attribute from an action servlet.
 
Similar Threads
how can you be sure abt session attribute from any other servlet in the same context
NullPointerException
How to get servlet context or similar object in Struts
working with drop down menus
Servlet Context