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

怎么在 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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?