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()%>是为了解决相对路径的问题,可返回站点的根路径. 但 ...
随机推荐
- 多台Mac电脑使用一个apple开发者账号
直接从已安装好的机器上导出私有密钥的,具体方法如下: Xcode的organizer的IPHONE DEVELOPMENT --->Developer Profile里自带的Export和Imp ...
- CSS3 概览 更新时间 2014-0412-1317
CSS3 概览 CSS3可以划分为:文字.边框模型.背景.动画等. CSS3颜色模块 CSS2.1的时候可以使用4种颜色方式,直接使用颜色名,如 redRGB值,如 rgb(0,90,255)RGB百 ...
- 《Numerical Methods》-chaper7-解线性方程组的直接方法和最小二乘问题
基于我们在线性代数中学习过的知识,我们知道解线性方程组本质上就是Gauss消元,也就是基于增广矩阵A的矩阵初等变换.关于数学层面的内容这里不做过多的介绍,这里的侧重点是从数值计算的角度来看这些常见的问 ...
- JVM中锁优化简介
本文将简单介绍HotSpot虚拟机中用到的锁优化技术. 自旋锁 互斥同步对性能最大的影响是阻塞的实现,挂起线程和恢复线程的操作都需要转入内核态中完成,这些操作给系统的并发性能带来了很大的压力.而在很多 ...
- poj 2253 Frogger【最小生成树变形】【kruskal】
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30427 Accepted: 9806 Descript ...
- 【转】java 文件 读取目录下的所有文件(包括子目录)
转自:http://www.cnblogs.com/pricks/archive/2009/11/11/1601044.html import java.io.File; import java.io ...
- 【Android - MD】之FloatingActionButton的使用
FloatingActionButton(FAB) 是 Android 5.0 新特性--Material Design 中的一个控件,是一种悬浮的按钮. FloatingActionButton 是 ...
- PPT扁平化手册 2
- Redis Error
1,MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. C ...
- MySQL Replication主从复制
MySQL Replication:NySQL复制,MySQL的复制默认为异步工作模式 mysql的复制功能是mysql内置的,装上它之后就具备了这个功能,而mysql复制是mysql实现大规模高 ...