getContextPath、getServletPath、getRequestURI,getRealPath的区别
假定你的web application 项目名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp
则执行下面向行代码后打印出如下结果:
1、 System.out.println(request.getContextPath()); //可返回站点的根路径。也就是项目的名字
打印结果:/news
2、System.out.println(request.getServletPath());
打印结果:/main/list.jsp
3、 System.out.println(request.getRequestURI());
打印结果:/news/main/list.jsp
4、 System.out.println(request.getRealPath("/"));
request.getRealPath("/")已经不建议使用。
getRealPath();
返回一个字符串,包含一个给定虚拟路径的真实路径。
struts2中:
ServletContext ctx=ServletActionContext.getServletContext();
String path=ctx.getRealPath("/");
String path1=ctx.getRealPath("/files/view.jsp");
输出:path为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\
path1为D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\files\view.jsp
\files\view.jsp这部分就是虚拟路径
D:\Javasoftware\apache-tomcat-7.0.70\apache-tomcat-7.0.70\webapps\struts2-1\ 为项目的绝对路径
servlet中:
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config=config;}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext ctx=config.getServletContext();
String temp=ctx.getRealPath("/");
}
打印结果:F:\Tomcat 6.0\webapps\news\test
注:
URI=contextPath+servletPath
getContextPath、getServletPath、getRequestURI,getRealPath的区别的更多相关文章
- Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")
假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...
- getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径
getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...
- 获得项目的绝对地址 getRequestURI,getRequestURL的区别
java获得tomcat项目的绝对地址 String basePath = request.getScheme()+"://"+request.getServerName()+&q ...
- getRequestURI,getRequestURL的区别,获取各种路径的方法
getRequestURI,getRequestURL的区别 test1.jsp======================= <a href ="test.jsp?p=fuck&qu ...
- getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...
- Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...
- request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- getServletPath getRequestURI getRequestURL区别
getContextPath:/test //上下文,类似工程名 getServletPath:/test.jsp getRequestURI:/test/test.jsp ...
- 关于J2EE里面getContextPath()和getRealPath()的区别
一直老搞不清楚这两个方法的区别,只知道他们都是拿来获取地址的.今天特意写了个小demo试了一下,代码如下: @Override protected void service(HttpServletRe ...
随机推荐
- React组件传值方式总结
1. 子组件向父组件传值 父组件Header: import Nav from 'Nav.js'; class Header extends React.Component { constructor ...
- android httpclient 设置超时
3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...
- mina中责任链模式的实现
一.mina的框架回顾 责任链模式在mina中有重要的作用,其中Filter机制就是基于责任链实现的. 从上图看到消息的接受从IoService层先经过Filter层过滤处理后最后交给IoHander ...
- web项目WebContent目录结构参考(WEB-INF)
WEB-INF目录是Java WEB应用的安全目录,客户端(浏览器等)无法访问,只有服务端可以访问.该目录主要用来存放配置文件,如web.xml等. 若是将jsp文件放在WEB-INF目录中,则必须通 ...
- 微信小程序之工具js封装与使用
工具库的创建与使用 创建一个common文件夹 在common文件夹中创建一个utils文件夹 在utils文件夹中创建util.js // 工具 function tool() { console. ...
- 微信小程序之if操作
.wxss控制样式 .price-agent{ font-size: 25rpx; color:#ababab; float: left; position: absolute; bottom: 0; ...
- PL/SQL 训练01--基础介绍
--开始介绍变量之前,我们先看下怎么在PLSQL写程序,如下我们写了一个块 declare --声明部分,声明变量 v_name ) :='hello world'; begin --执行区域 dbm ...
- Linux学习笔记 -- 初识 Shell
Shell 是什么 Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务.Shell既是 ...
- linux那点事儿(七)----文件系统管理
如果你是一位忠实的windows 用户,那么现在请你打开的的c盘,打开WINDWOS目录,下面存放了哪些文件和目录,相信没有人关心过吧!即便是用windows多年的人.额!其实,我也知道WINDOWS ...
- 前端自动化之webstrom
前端自动化之webstrom 在刚接触前端的时候,使用的就一直是webstrom,版本是webstrom 8. 前端自动画使用的时候,因为webstrom 8版本是没有集成gulp的.所以每次使用都默 ...