request.getContextPath获取绝对路径
项目需求:所有jsp页必须通过Action转发,不能直接在地址栏链接jsp或<a href="aaa.jsp"></a>
我的应用如下:
1.LoginFilter
- public class LoginFilter implements Filter {
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletRequest hRequest = (HttpServletRequest)request;
- HttpServletResponse hResponse = (HttpServletResponse)response;
- String uri = hRequest.getRequestURI();
- System.out.println("uri======"+uri);
- TBussinessInfo info = (TBussinessInfo) hRequest.getSession().getAttribute("user");
- if( "/b2cPlatform/jsp/phone/login/login.jsp".equals(uri) || "/b2cPlatform/".equals(uri)
- || "/b2cPlatform/jsp/phone/login/trunToLogin.jsp".equals(uri) )
- {
- chain.doFilter(request, response);
- }
- else
- {
- hResponse.sendRedirect(hRequest.getContextPath());//不允许直接访问jsp页,除非login.jsp等页
- }
- }
- public void destroy() {}
- public void init(FilterConfig arg0) throws ServletException {}
- }
2.web.xml
- <!-- 登录Filter -->
- <filter>
- <filter-name>LoginFilter</filter-name>
- <filter-class>com.hanpeng.b2c.phone.priv.LoginFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>LoginFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
二、网上其他资料
<%=request.getContextPath()%>是解决相对路径的问题,可返回站点的根路径。
<a href="<%=request.getContextPath()%>/XXX.jsp"> //这样获得的是绝对路径
<a href="XXX.jsp"> //这样获得的是相对路径
<a href="<%=request.getContextPath()%>/XXXX.jsp"> 能够更有效的防治连接的失效。
request.getContextPath()得到的是项目的名字,如果项目为根目录,则得到一个"",即空的字条串,
如果项目为dzjc, <%=request.getContextPath()% >/ 将得到dzjc/,服务器端的路径则会自动加上,
<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()
得到工程文件的实际物理路径,也就是绝对地址
简便用法:
String path=request.getContextPath();//path = /oa
request.setAttribute("path", path);
在页面中应用(EL) ${path}/images/xx.jpg(绝对定位:oa/images/xx.jpg)
可以解决复杂的相对路径定位失败的问题。
request.getContextPath获取绝对路径的更多相关文章
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
本随笔这是作为一个记录使用,以备后查.项目完成之后本地部署OK,本地Linux部署OK,都可以正常的访问,可是当我把它部署到服务器上面的时候,首页可以正常访问,可是当发出请求的时候却报错误了,说找不到 ...
- jsp中【<%=request.getContextPath()%>】项目路径
1 2 "request.getContextPath()的值是 "<%=request.getContextPath()%><br/> &q ...
- request 获取各种路径
从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...
- request获取各种路径
equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 在ser ...
- 从request获取各种路径总结
一.获得都是当前运行文件在服务器上的绝对路径 在servlet里用: this.getServletContext().getRealPath() 在struts用: this.getServlet( ...
- 从request获取各种路径总结 request.getRealPath("url")
转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath() 这个方法已经不推荐使用了,代替方法是 ...
- request.getContextPath是为了解决相对路径的问题,可返回站点的根路径
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- request获取各种路径总结、页面跳转总结。
页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...
- request.getcontextPath() 详解
request.getcontextPath() 详解 文章分类:Java编程 <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但 ...
随机推荐
- css两句话搞定漂亮表格样式
CSS代码: table { background-color:#c0de98; width:500px; height:100px; } td { background-color:#ffffff; ...
- ACM1228_STL的应用
#include<iostream> #include<string> #include<map> using namespace std; map<stri ...
- BCTF赛后
今天早上八点,BTCF第一届落幕.虽然没有熬夜coding但还是十分开心. 从收到超哥的邀请,到回头组队,一开始的十几人,最后演化为五人的精简小分队,到昨晚把所有有想法的题目全部搞定,十分的开心和欣慰 ...
- zabbix 飞信集成配置
1.环境说明: (Gentoo Linux环境) # uname -a Linux Crob 2.6.34-gentoo #1 SMP Mon May 31 15:45:25 CST 2010 x86 ...
- Border - SGU 133(排序)
题目大意:有N对区间现在剔除一些区间,这些区间被另一些区间完全包含,如,Ai<Bi, Bj<Aj, A完全包含B,求出来这样被包含的区间个数. 分析:首先按照第一个数字先进行一下排序,然后 ...
- Eclipse自动换行WordWrap插件
eclipse没有自动换行功能,需要安装插件wordwrap,方法请自行百度,可以参考下面的方法: http://jingyan.baidu.com/article/ce09321b7ba7042bf ...
- Java 执行jar linux 实例
需求:通过执行jar的方式 ,把某个文件路径下的用户数据同步到redis 1.main 函数 public class Main { private static Logger logger = Lo ...
- Cubieboard 开箱和入门 | Name5566 分类: cubieboard 2014-11-08 17:27 251人阅读 评论(0) 收藏
Cubieboard 开箱和入门 2014 年 01 月 29 日 by name5566 Categories: Computer Science, Cubieboard Hello Cubiebo ...
- hdoj Last non-zero Digit in N! 【数论】
找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...
- HTML5游戏开发技术基础整理
随着HTML5标准终于敲定.HTML5将有望成为游戏开发领域的的热门平台. HTML5游戏能够执行于包含iPhone系列和iPad系列在内的计算机.智能手机以及平板电脑上,是眼下跨平台应用开发的最佳实 ...