STRUST 项目中的 ServletConfig.getServletContext() 无法得到?

04-02-20 cute


今天在STRUST 项目中的 action 中
 用到了 request.getRealPath("/photo/"+name+".gif")
发现函数request.getRealPath 不建议使用,要求用ServletContext 中的这个方法。
可是这个类的实例要求用 ServletConfig.getServletContext() 才能获得。
之后,我发现只有继承implements Servlet 借口才能 的到ServletConfig 实例。
于是我就:

public class UserManagerAction extends Action implements Servlet{
...

public void init(ServletConfig config) throws ServletException {
this.config=config;
}

}
在其中,我实现这个congif! 并希望通过他得到ServletContext  实例,这样我得目的就达到了。
。。

没想到,最后运行时,却出现异常。异常的原因就发生在 :

ServletContext sc=config.getServletContext();

这。

请问,我怎么才能获得 ServletContext  实例? 

3
banq
2004-02-28 12:39

struts中ServletConfig有点绕人,建议打开struts源码研究一下。
ServletConfig好像有一个继承品。

cute
2004-03-05 07:55

这个问题最后是这样解决的:

request.getSession.getServletContext.getRealPath();

好不容易呀,就这样解决了.