File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Accessing ServletContext attribute from Action Class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing ServletContext attribute from Action Class." Watch "Accessing ServletContext attribute from Action Class." New topic

Accessing ServletContext attribute from Action Class.

wai chun chor

Joined: Sep 28, 2011
Posts: 2
Hi, I have a Struts 2 application where a servlet runs on startup, it saves my datasource config into a servletContext:

The problem is I could not get this value from my action class where i need this datasource connection.

Any help is appreciated.
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188


try this in your action
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

It's not better approach set as dataSource in webserver.

ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Struts Action objects are part of a Struts-based Controller. Ideally, you should not want to connect or access a data source from the Controller, i.e. Action objects.

As a treat, below is how to get the ServletContext object

It is sorta covered in the JavaRanch Style Guide.
subject: Accessing ServletContext attribute from Action Class.
It's not a secret anymore!