zoukankan      html  css  js  c++  java
  • 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)

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

  • 相关阅读:
    input 正则
    .net ashx Session 未将对象引用到实例
    js 时间和时间对比
    c# Repeater 和 AspNetPager
    c#后台 极光推送到Android 和IOS客户端
    select scope_identity()
    redhat7.4安装git(按照官网从源码安装)
    redhat7.4安装gitlab
    ES6模板字符串
    初次接触webpack
  • 原文地址:https://www.cnblogs.com/archermeng/p/7537569.html
Copyright © 2011-2022 走看看