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

2019-03-25 13:46|来源: 网路

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);
	}

问题补充:<div class="quote_title">梦中有你 写道</div><div class="quote_div">用这个: <br /> <br /><pre name="code" class="java">
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
</pre> <br /> <br />你的整个项目的的URL都可以获取 只要拿到request</div> <br /> <br />不行,取不到。而且这个方法只能取到,项目的目录,页面都取不到。 <br />path = "/PrxWebCache” (为项目根目录) <br />basePath = "http://localhost:81/PrxWebCache/" <br /> <br />对于我想要的&nbsp; /PrxWebCache/index.jsp 没法取到。

问题补充:<div class="quote_title">cwalet 写道</div><div class="quote_div">你明显定义的过滤页面是index.jsp为什么你还要取得这个url,你可以直接使用嘛. <br />而且你使用jsp:include,即使使用动态包含iframe也只能取得当前父窗口的url.</div> <br /> <br />情况是:在一个大的页面中,include了很多小的jsp或Action,我需要通过Fitler判断(根据URL),这个include进来的页面是否需要缓存。然后再进一步操作。 <br /> <br />首先从缓存URL配置中取得需要缓存的URL。 然后把请求的URL与需要缓存URL进行比较。去判断该页面是否需要缓存。 <br /> <br />一般Filter是不会拦截 在页面中的&lt;jsp:include&gt;URL请求。所以增加了Fitler配置。 <br />&lt;dispatcher&gt;request&lt;/dispatcher&gt; <br />使得Filter能够拦截。&nbsp; <br /> <br />在拦截到进入Fitler后,要先取得请求的URL,然后判断是否需要缓存。但是,就像我问的问题,使用request.getRequestURI();取不到该 include指向的URL。只去取到了,上面说的 “大的页面”的URL。&nbsp; <br /> <br />所以现在的问题是:怎么在Fitler中取include 指向的URL <br /> <br /> <br />

问题补充:<div class="quote_title">cwalet 写道</div><div class="quote_div">这样的话,你试试在jsp:include中加一个param子参数,value是包含的url作为参数传递给filter <br /><pre name="code" class="java">
&lt;jsp:param name= "*" value="*" /&gt;
</pre></div> <br /> <br />这个方案我想过,因为可以取到传参值。 <br /> <br />考虑这个方案,我现在需要拦截的是很多URL请求,我要判断出,该URL请求是,浏览器客户端的直接请求,还是来自页面中include方式的URL请求。 <br /> <br />那么则需要在每个include方式在URL请求中,增加一个特殊的参数(其他请求方式不含该参数名),用来标识该请求来自include,且值为该include的URL。 <br /> <br />按上面说的方案的确可行。 <br /> <br />问题就在于: <br />现在项目基本完成,只是需要增加缓存机制,所以需要一个方便的缓存框架(类似插件),加入该项目,通过配置使得合理缓存需要缓存的页面。 <br /> <br />使用该方案,需要修改的页面将会有很多,因为项目的页面很分散,有很到多个include。涉及到大量修改,不是理想方案。 <br /> <br />暂且做为 备选方案 吧。 呵呵。 <br /> <br />谢谢大家的意见。 <br /> <br />

问题补充: <br /> <br />哈哈, 我刚才已经测试出来了。 我知道从Filter取include的URL了。 <br />只要一句话就好了: <br /> <br />request.getAttribute("javax.servlet.include.request_uri") <br /> <br />如果Filter拦截的是浏览器请求的URL,则 该值为:null <br />如果拦截的是页面的incldue请求的URL,则 该值为:include的URL值。 <br /> <br />谢谢大家, 问题关闭了。 分就给cwalet吧, 毕竟回答了两次,呵呵。 <br />

相关问答

更多