The method getRealPath(String) from the type ServletRequest is deprecated
Published: November 13, 2011 , Updated: February 13, 2012 , Author: mkyong
See following example to get the real server file path via servletRequest.getRealPath("/"). However, warning is prompt and complained that this method is deprecated.
import javax.servlet.http.HttpServletRequest; public class DisplayAction { private HttpServletRequest servletRequest; public String execute() { //The method getRealPath(String) from the type ServletRequest is deprecated String filePath = servletRequest.getRealPath("/"); } @Override public void setServletRequest(HttpServletRequest arg0) { this.servletRequest = arg0; } }
Instead, you should use servletRequest.getSession().getServletContext().getRealPath("/") (refer to the end of the reference site for detail). See updated example again.
import javax.servlet.http.HttpServletRequest; public class DisplayAction { private HttpServletRequest servletRequest; public String execute() { servletRequest.getSession().getServletContext().getRealPath("/"); } @Override public void setServletRequest(HttpServletRequest arg0) { this.servletRequest = arg0; } }






