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

  1. getServletPath getRequestURI getRequestURL区别

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

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

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

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

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

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

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

  5. 转:getContextPath、getServletPath、getRequestURI的区别

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

  6. HttpServletRequest 中 getRequestURL和getRequestURI的区别

    比如说有这样的一个页面 test1.jsp======================= <a href ="test.jsp?name=wf">跳转到test2.js ...

  7. getRequestURI getRequestURL 区别

    参考 https://blog.csdn.net/gavid0124/article/details/45390999/ request.getRequestURL() 返回全路径 request.g ...

  8. request.getRequestURL()和request.getRequestURI()的区别

    request.getRequestURL() 返回全路径 request.getRequestURI() 返回除去host(域名或者ip)部分的路径 request.getContextPath() ...

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

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

随机推荐

  1. iOS8通讯录之联系人增删查,多号码增删操作

    #import <AddressBook/AddressBook.h> #pragma mark 删除一个号码 - (void)deleteLocalMarkSuccess:(void(^ ...

  2. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  3. .NET使用一般处理程序生成验证码

    运行的效果图: HTML的代码: <head> <script type="text/javascript"> function changeCode() ...

  4. 五、selecting with the API

    1. 命令通常从selection list中得到input, 调用MGlobal::getActiveSelectionList(MSelectionList &dest, bool ord ...

  5. Marathon

    早上,挣扎到十点才起,刚好去吃过饭,来教研室,等待着中午的马拉松. 中午一直是很激动,有些紧张.一直到整个马拉松跑完,我达到了我唯一的目标,保持均匀的呼吸节奏.但我能明显感觉到,我并没有拼尽全力.我不 ...

  6. openldap主机访问控制(基于用户组)

    建立组织单元 cat << _EOF_ | ldapadd -x -W -H ldaps://master.local -D cn=manager,dc=suntv,dc=tv dn: o ...

  7. 转:Delphi各种Socket组件的模式和模型

    Delphi的大多数书籍里面都没有提到delphi的各种socket通信组件的模式和模型,有的书只讲解了windows的socket模式和模型,并没有归纳各种组件采用的模型,所以我们的程序员并不知道如 ...

  8. linux tr命令详解

    通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符.您也可以用它来除去重复 ...

  9. bootstrap分页插件--Bootstrap Paginator的使用&AJAX版备份(可单独使用)

    html部分: <ul class="pagination"></ul> <!--bootstrap3版本用ul包裹--> <div cl ...

  10. phpstorm

    9XVKERIY9F-eyJsaWNlbnNlSWQiOiI5WFZLRVJJWTlGIiwibGljZW5zZWVOYW1lIjoiYXNoZXIgY2hlbiIsImFzc2lnbmVlTmFtZ ...