iteye_20450 2010-11-26 15:23
浏览 183
已采纳

怎么在 Fitler 中取得 jsp:include 中的URL

Sevlet2.4规范 可以支持 <jsp:include page="URL"> 中匹配的URL,通过Fitler 配置如下

 

<filter>
        <filter-name>Cache</filter-name>
        <filter-class>prx.cache.filter.CacheFilter</filter-class>
        <init-param>
            <!-- 过期时间设置,默认为60秒 -->
            <param-name>refreshPeriod</param-name>
            <param-value>120</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>Cache</filter-name>
        <url-pattern>/index.jsp</url-pattern>
        <dispatcher>request</dispatcher>
        <!-- 使得页面中通过 include 方式嵌入的匹配页面也可以通过该Filter -->
        <!-- 但是在Fitler中却取不到 include 指定的 url 值,只能取到原始含有该include的URL -->
        <dispatcher>include</dispatcher>
    </filter-mapping>

main.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    main context
    <jsp:include page="./index.jsp?type=test"></jsp:include>
</body>
</html>
 

在Filter中可以可以取到 index.jsp?type=test 传入的参数type的值"test",却得不到"/index.jsp"这个 URL

 

public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        
        //取得的值为:/main.jsp,而不是/index.jsp;怎么取到 /index.jsp呢?
        String url = httpRequest.getRequestURI();

        //可以取得传参:test
        String type = httpRequest.getParameter("type");
        
        chain.doFilter(request, response);
    }
  • 写回答

4条回答 默认 最新

  • iteye_14640 2010-11-26 17:21
    关注

    这样的话,你试试在jsp:include中加一个param子参数,value是包含的url作为参数传递给filter
    [code="java"]

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题