本文主要是介绍一文彻底搞清楚JavaWeb中request的路径区分(详解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JavaWeb中request的路径问题
- 1. request 中的路径分类
- 1.1 资源路径
- 1.2 部署路径
- 1.3 项目路径
- 1.4 URI 与 URL
- 2. 源码验证 + 输出截图
1. request 中的路径分类
项目名称:/demo
(IDEA中修改了当前Tomcat配置项目访问根目录为 /demo)
当前资源:/path
访问路径:http://127.0.0.1:8080/demo/path
1.1 资源路径
- request.getServletPath();
毋庸置疑为 Servlet 资源路径:
/path
1.2 部署路径
- request.getRealPath(“index.jsp”); // 过时了
- request.getServletContext().getRealPath(“index.jsp”);
- request.getSession().getServletContext().getRealPath(“index.jsp”);
getRealPath(String path) 统一是部署路径:
磁盘路径\out\artifacts\demo_job_war_exploded\index.jsp
应用:
- 上传文件路径:xxx.getRealPath(“uploadDir”) + File.separator + uploadFilename
1.3 项目路径
- this.getServletContext().getContextPath();
- request.getContextPath();
- request.getServletContext().getContextPath();
- request.getSession().getServletContext().getContextPath();
getContextPath() 统一是项目路径:
/demo
应用:
- 重定向路径: request.getContextPath() + servletPath或htmlPath
- 下载文件路径:request.getContextPath() + File.separator + “uploadDir” + File.separator + uploadFilename
1.4 URI 与 URL
- request.getRequestURI();
- request.getRequestURL();
访问:http://127.0.0.1:8080/demo/path 验证结果:
URI 路径为:/demo/path
(当前 Servlet 在项目的绝对路径)
URL 路径为:http://127.0.0.1:8080/demo/path
(当前 Servlet 在项目中带协议和地址端口的绝对路径)
总结:URL = 协议 + ip:port + URI
2. 源码验证 + 输出截图
@WebServlet(name = "TestPathServlet", urlPatterns = "/path")
public class TestPathServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/* 项目名称:/demo 当前资源:/path */// 毋庸置疑 ServletPath 为 "/path"String servletPath = request.getServletPath();// getRealPath(String path) 统一是部署路径: "磁盘路径\out\artifacts\demo_job_war_exploded\index.jsp"// 应用:// 1. 上传文件路径:xxx.getRealPath("uploadDir") + File.separator + uploadFilenameString realPath1 = request.getRealPath("index.jsp");String realPath2 = request.getServletContext().getRealPath("index.jsp");String realPath3 = request.getSession().getServletContext().getRealPath("index.jsp");// getContextPath() 统一是项目路径: "/demo"// 应用:// 1. 重定向路径: request.getContextPath() + servletPath或htmlPath// 2. 下载文件路径:request.getContextPath() + File.separator + "uploadDir" + File.separator + uploadFilenameString contextPath = this.getServletContext().getContextPath();String contextPath1 = request.getContextPath();String contextPath2 = request.getServletContext().getContextPath();String contextPath3 = request.getSession().getServletContext().getContextPath();// 访问:http://127.0.0.1:8080/demo/path得出的结果String remoteUser = request.getRemoteUser(); // nullint localPort = request.getLocalPort(); // 8080String localAddr = request.getLocalAddr(); // 127.0.0.1String remoteAddr = request.getRemoteAddr(); // 127.0.0.1int remotePort = request.getRemotePort(); // 8080// /demo/path (当前 Servlet 在项目的绝对路径)String requestURI = request.getRequestURI();// http://127.0.0.1:8080/demo/path (当前 Servlet 在项目中带协议和地址端口的绝对路径)StringBuffer requestURL = request.getRequestURL();}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}
}
输出截图:
这篇关于一文彻底搞清楚JavaWeb中request的路径区分(详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!