The method getRealPath(String) from the type ServletRequest is deprecated

The method getRealPath(String) from the type ServletRequest is deprecated

出现该警告,该方法已经不能使用了

Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead.

可以使用下面的方法来替代上面的方法

request.getSession().getServletContext().getRealPath() 得到站点的绝对地址

在servlet或者struts中还可以这样:

this.getServletContext().getRealPath("/");

this.getServlet().getServletContext().getRealPath("/");


对文件操作时不能是相对路径,必须是绝对路径,与src="" url=""不同,后者是相对路径。

你可能感兴趣的:(servlet)