`
yin007008
  • 浏览: 3561 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

request.getcontextPath() 详解

 
阅读更多


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



 

  

参考

servlet

中的接口:



  

request.getScheme();

 

返回的协议名称

,

默认是

http



  

request.getServerName()

 

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



  

getServerPort()



获取服务器端口号

  









 

  

request.getContextPath()

应该是得到项目的名字,

如果项目为根目录,

则得到

一个

""

,即空的字条串。如果项目为

abc,

<%=request.getContextPath()%

>



得到

abc

,服务器端的路径则会自动加上,

<a

href="XXXX.jsp">

是指当前路径

下的这个

xxx.jsp

页面,有时候也可以在

head

里设置

html:base

来解决路径的

问题,不过用的最多的还是

request.getContextPath





  



js

文件中得到

request.getContextPath()

的值,不想在

JSP

中写太多的

Javascript

代码:



一种方法是用

hidden: 

<input

type=hidden

name=contextPath

value=<%=

request.getContextPath()>

 

  

再就是在本页面的

js

里面也是可以使用

Scriptlet

来赋值的:



var a = '<%= request.getContextPath()>' 

 

或者你赋值给一个

hidden

的控件都是可以的,然后

js

取,这样

js

可以不用写



jsp

里。



  

实际应用中,一般用来解决

jsp

测试和生产环境路径不同的问题:

 

<% 

String appContext = request.getContextPath();

String basePath = 

request.getScheme()+"://"+request.getServerName()+":"+

request.getServerPort() + appContext; 

%>











 
分享到:
评论

相关推荐

    request.getcontextPath()_详解

    request.getcontextPath()详解

    java中request.getcontextPath()的意思.doc

    java中request.getcontextPath()的意思.doc

    数据库测试test.sql

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

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

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

    新闻推荐系统

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

    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); }

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

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

    最简单js代码实现select二级联动下拉菜单

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

    网页视频播放插件

    var so = new SWFObject("&lt;%=request.getContextPath()%&gt;/js/player/CuPlayerMiniV4.swf","CuPlayerV4","600","410","9","#000000"); so.addParam("allowfullscreen","true"); so.addParam("allowscriptaccess",...

    request的使用.txt

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

    Java后端实现websocket与微信小程序端连接简单例子

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

    DWR开发包常规客户端服务器端面向对象编程

    dwr开发包,常规客户端...&lt;script type='text/javascript' src='&lt;%=request.getContextPath()%&gt;/dwr/interface/TestService.js'&gt; &lt;script type='text/javascript' src='&lt;%=request.getContextPath()%&gt;/dwr/engine.js'&gt; ...

    WEB15-request.rar

    String getContextPath() ---web应用的名称 String getQueryString() ---- get提交url地址后的参数字符串 username=zhangsan&password=123 注意:request获得客户机(客户端)的一些信息 request.getRemoteAddr() -...

    用户管理系统(ums)

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

    My97DatePicker日期控件项目应用

    My97DatePicker日期控件使用引入js脚本目录,注意必须用&lt;%=request.getContextPath() %&gt;来获取项目根目录+脚本目录的方式,否则日期控件会无效!!! &lt;script type="text/javascript" src="&lt;%=request....

    Request中getContextPath、getServletPath的区别

    NULL 博文链接:https://javachikuang.iteye.com/blog/262264

    jsp中获取地址的写法

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

    登录过滤器

    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的request对象实例详解

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

Global site tag (gtag.js) - Google Analytics