request.getContextPath获取绝对路径

项目需求:所有jsp页必须通过Action转发,不能直接在地址栏链接jsp或<a href="aaa.jsp"></a>

 

我的应用如下:

1.LoginFilter

Java代码   收藏代码
  1. public class LoginFilter implements Filter {  
  2.   
  3.     public void doFilter(ServletRequest request, ServletResponse response,  
  4.             FilterChain chain) throws IOException, ServletException {  
  5.           
  6.         HttpServletRequest hRequest = (HttpServletRequest)request;  
  7.         HttpServletResponse hResponse = (HttpServletResponse)response;  
  8.           
  9.         String uri = hRequest.getRequestURI();  
  10.         System.out.println("uri======"+uri);  
  11.           
  12.         TBussinessInfo info =  (TBussinessInfo) hRequest.getSession().getAttribute("user");  
  13.           
  14.         if"/b2cPlatform/jsp/phone/login/login.jsp".equals(uri) || "/b2cPlatform/".equals(uri)   
  15.                 || "/b2cPlatform/jsp/phone/login/trunToLogin.jsp".equals(uri) )  
  16.         {  
  17.             chain.doFilter(request, response);  
  18.         }  
  19.         else  
  20.         {  
  21.             hResponse.sendRedirect(hRequest.getContextPath());//不允许直接访问jsp页,除非login.jsp等页  
  22.         }  
  23.     }  
  24.       
  25.     public void destroy() {}  
  26.   
  27.     public void init(FilterConfig arg0) throws ServletException {}  
  28. }  

 2.web.xml

Xml代码   收藏代码
  1. <!-- 登录Filter -->  
  2. <filter>  
  3.     <filter-name>LoginFilter</filter-name>  
  4.     <filter-class>com.hanpeng.b2c.phone.priv.LoginFilter</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>LoginFilter</filter-name>  
  8.     <url-pattern>*.jsp</url-pattern>  
  9. </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)

可以解决复杂的相对路径定位失败的问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值