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); }
这样的话,你试试在jsp:include中加一个param子参数,value是包含的url作为参数传递给filter
[code="java"]
[/code]
用这个:
[code="java"]
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
[/code]
你的整个项目的的URL都可以获取 只要拿到request
你明显定义的过滤页面是index.jsp为什么你还要取得这个url,你可以直接使用嘛.
而且你使用jsp:include,即使使用动态包含iframe也只能取得当前父窗口的url.
我可以给你个建议,对类似这种问题,单从jsp是看不到问题根源的。你可以看看%tomcat_home%\work\Catalina\localhost\yourwebapp\main_jsp.java,看里面代码是怎么写的。