This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Servletcontext object to actionservlet in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Servletcontext object to actionservlet in struts" Watch "Servletcontext object to actionservlet in struts" New topic
Author

Servletcontext object to actionservlet in struts

Julie Jacob
Greenhorn

Joined: Aug 17, 2005
Posts: 20
how to pass Servletcontext object to actionservlet in struts
Aro Rajesh
Greenhorn

Joined: Sep 22, 2005
Posts: 3
Hi,

you can use init() method by passing servletconfig as a argument. from servlet config the context object can be derived as follows.

[code]
public void init(ServletConfig cfg) throws ServletException
{
sc = cfg.getServletContext();
}
[code]
Julie Jacob
Greenhorn

Joined: Aug 17, 2005
Posts: 20
this for a customServlet

Public synchronized void init(ServletConfig config){
MyROOT = config.getInitParameter( �MyROOT�);
Try{
super.init(config);
}catch (ServletException se) {
System.out.println(�Could not access super class init�);
}

If(MyROOT == null){
MyROOT =�The default App root directory where the configuration file is stored�
}

Properties props = new Properties(); // empty list

// Can be used if needed later //
//URL fileUrl = new URL("http://" + getCodeBase().getHost() + "/test/config.txt");//

URL fileUrl = new URL(MyROOT +�/config.txt");

InputStream in = fileUrl.openStream();

props.load(in);

}

myActionServlet extends CustomServlet

String desease = new String(props.getProperty(desease));

Here i needed initialise a values of Listbox
to config.txt.

in my web.xml file
<servlet-name>MyServletName</servlet-name>
<servlet-class>com.mycompany.MyServlet</servlet-class>

<init-param>
<param-name> param1 </param-name>
<param-value> value1 </param-value>
</init-param>


How this posible?
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I would be believe if your CustomServlet has a super class of HttpServlet, you should be able to access the ServletContext by simply calling the getServletContext() method or thru the init(ServletConfig config) method.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servletcontext object to actionservlet in struts
 
Similar Threads
How to get context-param from web.xml in struts app
accessing beans, declared in spring-config.xml
ActionClass.execute
how to override action servlet in struts
TC4-TC5.5 with context-param