现在的位置: 首页 > 综合 > 正文

servlet中获得tomcat项目相对路径的绝对路径

2013年05月13日 ⁄ 综合 ⁄ 共 531字 ⁄ 字号 评论关闭
由于JAVA2里不提倡使用 request.getRealPath()方法了 所以我们用 以下方法来实现操作
servlet中获得tomcat项目相对路径的绝对路径

public class CreateXmlAction extends HttpServlet {

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

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String  filePath = config.getServletContext().getRealPath("/");
 }

}

或者直接在继承了HttpServlet的Servlet中写:

String  filePath=this.getServletConfig().getServletContext().getRealPath("/");

方便文件操作了。

 

抱歉!评论已关闭.