假定你的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的区别的更多相关文章

  1. Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")

    假定web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果: ...

  2. getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径

    getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...

  3. 获得项目的绝对地址 getRequestURI,getRequestURL的区别

    java获得tomcat项目的绝对地址 String basePath = request.getScheme()+"://"+request.getServerName()+&q ...

  4. getRequestURI,getRequestURL的区别,获取各种路径的方法

    getRequestURI,getRequestURL的区别 test1.jsp======================= <a href ="test.jsp?p=fuck&qu ...

  5. getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

    1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...

  6. Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

    1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...

  7. request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别

    假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...

  8. getServletPath getRequestURI getRequestURL区别

    getContextPath:/test               //上下文,类似工程名 getServletPath:/test.jsp getRequestURI:/test/test.jsp ...

  9. 关于J2EE里面getContextPath()和getRealPath()的区别

    一直老搞不清楚这两个方法的区别,只知道他们都是拿来获取地址的.今天特意写了个小demo试了一下,代码如下: @Override protected void service(HttpServletRe ...

随机推荐

  1. 【java规则引擎】drools6.5.0版本中kmodule.xml解析

    kmodule.xml文件存放在src/main/resources/META-INF/文件夹下. <?xml version="1.0" encoding="UT ...

  2. 洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom

    传送门 题目大意:形成一个环的牛可以跳舞,几个环连在一起是个小组,求几个小组. 题解:tarjian缩点后,求缩的点包含的原来的点数大于1的个数. 代码: #include<iostream&g ...

  3. 服务端获得客户端ip

    /** * 获取客户端ip地址(可以穿透代理) * * @param request * @return */ public static String getRemoteAddr(HttpServl ...

  4. Chrome 的审查元素功能有哪些奇技淫巧

    学习地址: https://www.zhihu.com/question/34682699

  5. OpenCL™ 2.0 – Pipes

    copy from http://developer.amd.com/community/blog/2014/10/31/opencl-2-0-pipes/ OpenCL™ 2.0 – Pipes I ...

  6. 项目中使用的图片上传方法,base64存本地

    //生成健康报告 public function uploadJkbg(Request $r) { $data = $r->all(); $jkbg['jkbg_ctime'] = time() ...

  7. spring-aop + memcached 的简单实现

    一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...

  8. EasyUI TreeJson

    1. TreeJson str = GetTreeJsonByTable(dt, "); StringBuilder treeResult = new StringBuilder(); St ...

  9. 第十二章 MySQL触发器(待续)

    ······

  10. hadoop 小知识点

    ------------------------------------------- 配置hadoop的最小blocksize,必须是512的倍数. [hdfs-site.xml] dfs.name ...