aspose file tools*
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servletcontext object to actionservlet in struts