jsp中的request.getContextPath()
 

<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。

但不用也可以吧,比如<a href="<%=request.getContextPath()%>/catalog.jsp">
我就直接用<a href="catalog.jsp">也行啊,这两个文件是在同一个目录下的

比如你要生成一个文件放在服务器上得一个目录下,你可以使用request.getContextPath()+/dir,组成一个完整得目录结构!

但在JSP文件里,通过request.getContextPath()得到的路径却为空,为什么?

你在context中没有配置path属性,所以你的工程文件就是在根目录下,相当于path="";即是你直接在 浏览器中输入你的服务器ip就会到你的jsp页面,而不是tomcat的默认页面;所以你通过request.getContextPath()得到的字 符串是为空的;它是获得虚目录的;
如果你想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web

request.getScheme();
返回的协议名称,默认是http

request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了

getServerPort()
获取服务器端口号

假定你的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("/"));

打印结果:F:\Tomcat 6.0\webapps\news\test

     
 
 

jsp中的request.getContextPath()的更多相关文章

  1. jsp中【<%=request.getContextPath()%>】项目路径

    1 2 "request.getContextPath()的值是        "<%=request.getContextPath()%><br/> &q ...

  2. 此时servlet中的request和我们在页面jsp中的request 是同一个request.

    在tomcat容器启动的时候,jsp页面的内置对象request,response,同样是依赖于tomcat容器中的servlet-api.jar包,这个jar包和我们在web项目中的jar包是一样的 ...

  3. jsp中${pageContext.request.contextPath}的意思

    ${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> . 也就是取出部署的应用程 ...

  4. JSP 中的 Request 和 Response 对象

    客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例:response对象包含了响应客户请求的有关信息,但在JSP中 ...

  5. jsp中的request.setAttribute的使用

    1.第一个jsp页面 <form id="form1" method="post" action="first.jsp"> &l ...

  6. request.getContextPath()

    今天终于明白了jsp中的request.getContextPath()是怎么回事了. request.getContextPath()  返回站点的根目录 request.getRealpath(& ...

  7. request.getcontextPath() 详解

    request.getcontextPath() 详解 文章分类:Java编程 <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但 ...

  8. 【转】JSP中的相对路径和绝对路径

    1.首先明确两个概念: 服务器路径:形如:http://192.168.0.1/的路径 Web应用路径:形如:http://192.168.0.1/yourwebapp的路径 2.关于相对路径与绝对路 ...

  9. jsp中如何获得url路径和绝对路径

    jsp中如何获得url路径 request.getHeader("referer") JSP中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径:request.getReq ...

随机推荐

  1. Leetcode——53.最大子序和

    @author: ZZQ @software: PyCharm @file: leetcode53_最大子序和.py @time: 2018/11/26 12:39 要求:给定一个整数数组 nums ...

  2. 第二个spring

    由于第一个spring已经完成,我们现在进去第二个spring! 陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法   plan好布局,分配任务,控制时间!

  3. 【转】使用screw plus对PHP源码加密

    运行环境 ubuntu 14.04 php 5.6 源码地址 https://github.com/del-xiong/screw-plus http://git.oschina.net/splot/ ...

  4. PAT 1082 射击比赛

    https://pintia.cn/problem-sets/994805260223102976/problems/994805260990660608 本题目给出的射击比赛的规则非常简单,谁打的弹 ...

  5. Spring Framework: @RestController vs @Controller

    https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annota ...

  6. Eclipse: Difference between clean, build and publish

    https://stackoverflow.com/questions/5656989/eclipse-difference-between-clean-build-and-publish http: ...

  7. Oracle 数据表误删恢复 Flashback

    1. 前提条件. recyclebin 参数打开. 验证参数是否打开: SHOW PARAMETER RECYCLEBIN 2. 如果参数没有打开的话 需要打开,并且重启一下数据库方法为 alter ...

  8. Jquery Ajax xml版Get请求PHP

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  9. node upgrade bug & node-sass

    node upgrade bug & node-sass bug solution rebuild $ npm rebuild node-sass OK

  10. 设计模式笔记:策略模式(Strategy)

    1. 策略模式简介 1.1 定义 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离,手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法,实现方法要根据具体的环境 ...