`
webcode
  • 浏览: 5951371 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    request.getcontextPath()_详解

    request.getcontextPath()详解

    数据库测试test.sql

    response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //...

    新闻推荐系统

    所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...

    JSP实现简单的用户登录并显示出用户信息的方法

    本文实例讲述了JSP实现简单的用户登录并...String path = request.getContextPath();  String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  %&gt; 

    Android代码-YuanNews

    所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...

    很经典的登录回选。

    if (session.getAttribute("person") == null) ... response.sendRedirect(request.getContextPath() + "/login.jsp"); }else{ request.getRequestDispatcher("fall.jsp").forward(request, response); }

    网页视频播放插件

    最近做的一个项目,网页中需要播放视频。 这里包括 我自己做的java工程的jsp里的 demo 和 js插件包..。 不用下载其他播放插件,轻巧简捷 只需两步: 1.把代码全部复制,更改一下视频路径 2.把js包放入项目里。然后...

    request的使用.txt

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; ``` 该语句用于获取当前网页的相对路径 例如: 访问...

    囧囧代码jsp代码jsp

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; &lt;!DOCTYPE ...

    jsp代码jsp代码

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; &lt;!DOCTYPE ...

    JSP实现计算器功能(网页版)

    String path = request.getContextPath(); String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/; %&gt; &lt;!DOCTYPE HTML PUBLIC -//W3C//DTD HTM

    jsp中获取地址的写法

    String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 的详解

    jsp获取url路径的方法分析

    本文实例讲述了jsp获取url路径的方法。分享给大家供大家参考,具体如下: 如果你请求的URL是 ...request.getContextPath() //输出: /demo request.getRequestPath() //输出: /Index.jsp request.gerRequestURI() /

    jQuery实现的点击按钮改变样式功能示例

    分享给大家供大家参考,具体如下: 1. css代码:index4.css @CHARSET "UTF-8"; .sd{ font-weight: bold; color: black;... background: red;...Stringpath = request.getContextPath(); StringbasePath = reque

    jsp中获得路径的两种方法和获得url路径的方法(推荐)

    =request.getContextPath()%&gt;是解决相对路径的问题,可返回站点的根路径。 &lt;a&gt;/XXX.jsp”&gt; //这样获得的是绝对路径 &lt;a&gt; //这样获得的是相对路径 &lt;a&gt;/XXXX.jsp”&gt; 能够更有效的防治连接的失效。 request....

    用户管理系统(ums)

    用户管理系统。语言JAVA public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, ... response.sendRedirect(request.getContextPath()+"/manager.jsp"); }

    JSP的request对象实例详解

    JSP的request对象实例详解 一 request对象定义 客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求...

    登录过滤器

    response.sendRedirect(request.getContextPath()); }else{ chain.doFilter(request, response); } } web.xml配置: &lt;filter-name&gt;accessFilter &lt;filter-class&gt;com.loginFilter&lt;/filter-class&gt; ...

    JSP的Cookie在登录中的使用

    JSP的Cookie在登录中的使用 一 功能需求 实现记忆用户名和密码功能。  二 代码 1、login.jsp &lt;&#37;@ page language=java import=java.util.*,java.net.* contentType=text/html; charset=utf-8%&gt; &lt;&#37...

    在jsp页面如何获得url参数

    String path = request.getContextPath(); String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”; String name = request.getParameter(“name”);//...

Global site tag (gtag.js) - Google Analytics