2011-08-13 41 views

回答

0

我建议不要将文件保留在该位置,因为这样可以很容易地使用http://yourURl/applicationContext/Config/myxml.xml公开访问,例如。我想你应该将它移动到WEB-INF文件夹下。

你应该能够做到像下面(快代码在运行中不抱我吧): -

public ActionForward foo(HttpServletRequest request, HttpServletResponse response){ 
    ServletContext context = request.getSession().getServletConext(); 
    InputStream stream = context.getResourceAsStream("Config/myxml.xml"); 
    if(stream!=null){ 
     //load your XML file from the stream 
    } 
} 

如果您正在使用Struts2的,那么你可以访问使用ServletActionContext作为ServletContext中好: -

ServletContext context = ServletActionContext.getServletContext(); 
相关问题