request的几个方法小总结:
工程目录结构:
logger.debug("request.getContextPath地址:" + request.getContextPath());
logger.debug("request.getSession().getServletContext().getRealPath:" + request.getSession().getServletContext().getRealPath(""));
logger.debug("request.getLocalAddr:" + request.getLocalAddr());
logger.debug("request.getRequestURI:" + request.getRequestURI());
结果:
[11 19:47:22,090 DEBUG] [http-bio-8080-exec-3] web.IndexController - request.getContextPath地址:/snf-helloworld-sample
[11 19:47:22,091 DEBUG] [http-bio-8080-exec-3] web.IndexController - request.getSession().getServletContext().getRealPath:D:\sts\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\snf-helloworld-sample
[11 19:47:22,092 DEBUG] [http-bio-8080-exec-3] web.IndexController - request.getLocalAddr:10.22.2.74
[11 19:47:22,093 DEBUG] [http-bio-8080-exec-3] web.IndexController - request.getRequestURI:/snf-helloworld-sample/admin/barCode.action
一句话:获取绝对路径常用的是:
request.getSession().getServletContext().getRealPath("")