web工程中和路径资源相关的方法及测试
2017-10-08 12:34
281 查看
httpreservlet中和路径相关的方法
request.getContextPath();
返回请求使用的实际上下文路径
request.getServletPath();
request.getRequestURL();
request.getRequestURI();
ServletContext中和路径相关的方法
servletContext servletContext = getServletContext();
补充1:测试代码
补充2: 测试结果及说明
①request.getContextPath()
/JD1706_Servlet
获得上下文路径
②request.getServletPath()
/TestServletPath
返回该请求使用的实际上下文路径
③request.getRequestURL()
http://localhost:8088/JD1706_Servlet/TestServletPath
获得浏览器地址
④request.getRequestURI()
/JD1706_Servlet/TestServletPath
项目中的路径
⑤getServletContext()
org.apache.catalina.core.ApplicationContextFacade@608061bd
上下文对象
⑥context.getContextPath()
/JD1706_Servlet
上下文路径
⑦context.getRealPath(‘/’)
E:\Study\javaee\apache-tomcat-6.0.18\webapps\JD1706_Servlet\
获得一个绝对路径/代表项目名后的/
⑧context.getResourcePaths(‘/’)
[/META-INF/, /download/, /WEB-INF/, /dabang.jpg, /zhaohongli.jpg, /form/, /upload/]
获得一个路径下的所有资源
request.getContextPath();
返回请求使用的实际上下文路径
request.getServletPath();
request.getRequestURL();
request.getRequestURI();
ServletContext中和路径相关的方法
servletContext servletContext = getServletContext();
servletContext.getContextPath(); servletContext.getRealPath("/"); servletContext.getREsourcePaths("/");
补充1:测试代码
package GQ.work; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServletPath extends HttpServlet { private static final long serialVersionUID = 1L; public TestServletPath() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); System.out.println("request.getContextPath()"+request.getContextPath()); System.out.println("request.getServletPath()"+request.getServletPath()); System.out.println("request.getRequestURL()"+request.getRequestURL()); System.out.println("request.getRequestURI()"+request.getRequestURI()); ServletContext context = getServletContext(); System.out.println("getServletContext()"+context); System.out.println("context.getContextPath()"+context.getContextPath()); System.out.println("context.getRealPath('/')"+context.getRealPath("/")); System.out.println("context.getResourcePaths('/')"+context.getResourcePaths("/")); } }
补充2: 测试结果及说明
①request.getContextPath()
/JD1706_Servlet
获得上下文路径
②request.getServletPath()
/TestServletPath
返回该请求使用的实际上下文路径
③request.getRequestURL()
http://localhost:8088/JD1706_Servlet/TestServletPath
获得浏览器地址
④request.getRequestURI()
/JD1706_Servlet/TestServletPath
项目中的路径
⑤getServletContext()
org.apache.catalina.core.ApplicationContextFacade@608061bd
上下文对象
⑥context.getContextPath()
/JD1706_Servlet
上下文路径
⑦context.getRealPath(‘/’)
E:\Study\javaee\apache-tomcat-6.0.18\webapps\JD1706_Servlet\
获得一个绝对路径/代表项目名后的/
⑧context.getResourcePaths(‘/’)
[/META-INF/, /download/, /WEB-INF/, /dabang.jpg, /zhaohongli.jpg, /form/, /upload/]
获得一个路径下的所有资源
相关文章推荐
- web中jsp获取工程路径方法
- 【Web】HttpServletRequest request 相关方法 得到路径
- 转:获取JAVA[WEB]项目相关路径的几种方法
- 获取JAVA[WEB]项目相关路径的几种方法(转载)
- java 工程获取路径 web根目录。 项目classpath根目录 各种实用方法 摘抄收藏
- Web、Java源代码、资源文件在Maven工程中的文件路径
- 关于 web应用程序建立在非默认路径【wwwroot】的方法 vs2002 vs2003 相关
- 获取JAVA[WEB]项目相关路径的几种方法
- java 工程获取路径 web根目录。项目classpath根目录 各种实用方法
- 在web页面里写引用资源绝对路径的替代方法
- web工程常用路径的获取方法
- java 工程获取路径 web根目录。 项目classpath根目录 各种实用方法 摘抄收藏
- java 工程获取路径 web根目录。 项目classpath根目录 各种实用方法 摘抄收藏
- 【转帖】【Web】HttpServletRequest request 相关方法 得到路径
- web工程在eclipse中的类编译路径与发布后的类编译路径问题和web工程中的资源文件读写问题
- 获取JAVA[WEB]项目相关路径的几种方法
- Genexus中取得Web工程路径的2种方法
- java 工程获取路径 web根目录。 项目classpath根目录 各种实用方法
- 获取JAVA[WEB]项目相关路径的几种方法
- Struts2 访问Action和Jsp页面出现的路径问题解决方案之 ---- 为jsp、图片、js、css等获取Web工程的绝对路径的方法