`
_ICE
  • 浏览: 5993 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

request.getContextPath(),路径,request.getRealPath

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

<a href="<%=request.getContextPath()%>/XXX.jsp"> //这样获得的是绝对路径

<a href="XXX.jsp"> //这样获得的是相对路径

<a href="<%=request.getContextPath()%>/XXXX.jsp"> 能够更有效的防治连接的失效。

request.getContextPath()得到的是项目的名字,如果项目为根目录,则得到一个"",即空的字条串,
如果项目为projectA, <%=request.getContextPath()% >/ 将得到projectA/,服务器端的路径则会自动加上,
<a href="XXX.jsp"> 应该就是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base
来解决路径的问题,不过用的最多的还是request.getContextPath。

用EL来表示相同功能的EL为:${pageContext.request.getContextPath()}

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

request.getServerName()
返回的是你浏览器中显示的主机名

getServerPort()
获取服务器端口号

如果想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web



现在request.getRealPath("") 这个方法已经不推荐使用了
可以使用
ServletContext.getRealPath(java.lang.String) instead. request.getSession().getServletContext().getRealPath() 得到工程文件的实际物理路径,也就是绝对地址
分享到:
评论

相关推荐

    request.getcontextPath()_详解

    request.getcontextPath()详解

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

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

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

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

    数据库测试test.sql

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

    新闻推荐系统

    基于用户兴趣标签的新闻推荐系统(毕业设计) 注意事项 界面的修改,均已index.jsp修改为主,在进行子界面的修改,保持统一性!...注意事项,jsp页面中的img标签添加获取项目名路径 ( request.getContextPath() ) 

    Android代码-YuanNews

    注意事项,jsp页面中的img标签添加获取项目名路径 ( request.getContextPath() )  原理图 架构-服务端 架构-客户端 主页-查询思路 推荐策略(后台) 推荐显示(前台) 推荐通知原理(Android) 爬虫原理 总的...

    很经典的登录回选。

    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

    **request.getContextPath()**返回虚拟路径: /hansom **request.getScheme()**返回使用的协议:http **request.getServerName()**返回所在服务器的名字:localhost **request.getServerPort()**返回IP端口:8080...

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

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

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

    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

    登录过滤器

    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获取url路径的方法分析

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

Global site tag (gtag.js) - Google Analytics