首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Filter阻截include方式的请求

2012-09-08 
Filter拦截include方式的请求又发现一个问题:在Filter中拦截到incldue方式的请求后,在取得传入的所有参数

Filter拦截include方式的请求

又发现一个问题:

在Filter中拦截到incldue方式的请求后,在取得传入的所有参数的时候,会把request 的请求参数也读出来。

比如:请求页面? /index.jsp?type1=request&param1=test1? 然后再 index.jsp页面中有一个 <jsp:include page="/main.jsp?type2=include&param2=test2"/>

当访问 /index.jsp?type1=request&param1=test1 时,使用上面的方法拦截到? include请求 /main.jsp?type2=include&param2=test2

Filter中的 在取得该请求的所有参数的时候,会将 /index.jsp的参数(type1=request&param1=test1)也读出来

  1. /**??*?返回请求的URL的参数部分,例如:url=/index.jsp?type=1&searchWord=java,则返回type=1&searchWord=java?
  2. ?*?@param?request??*/? ????private?static?String?getSortedQueryString(HttpServletRequest?request)?{?
  3. ????????Map?paramMap?=?request.getParameterMap();??
  4. ????????if?(paramMap.isEmpty())?{?????????????return?null;?
  5. ????????}??
  6. ????????Set?paramSet?=?new?TreeMap(paramMap).entrySet();??
  7. ????????StringBuffer?buf?=?new?StringBuffer();??
  8. ????????boolean?first?=?true;??
  9. ????????for?(Iterator?it?=?paramSet.iterator();?it.hasNext();)?{?????????????Map.Entry?entry?=?(Map.Entry)?it.next();?
  10. ????????????String[]?values?=?(String[])?entry.getValue();??
  11. ????????????for?(int?i?=?0;?i?<?values.length;?i++)?{?????????????????String?key?=?(String)?entry.getKey();?
  12. ?????????????????if?((key.length()?!=?10)?||?!"jsessionid".equals(key))?{?
  13. ????????????????????if?(first)?{?????????????????????????first?=?false;?
  14. ????????????????????}?else?{?????????????????????????buf.append('&');?
  15. ????????????????????}??
  16. ????????????????????buf.append(key).append('=').append(values[i]);?????????????????}?
  17. ????????????}?????????}?
  18. ?????????//?We?get?a?0?length?buffer?if?the?only?parameter?was?a?jsessionid?
  19. ????????if?(buf.length()?==?0)?{?????????????return?null;?
  20. ????????}?else?{?????????????return?buf.toString();?
  21. ????????}?????}
该方法返回的结果为: /main.jsp?type2=include&param2=test2&type1=request&param1=test1

这样就取得了包含,index.jsp的参数。

我现在想要只想要取得 include请求的参数,即 /main.jsp?type2=include&param2=test2 的参数部分type2=include&param2=test2

但是通过 request.getParameter()方法,是无法判断出哪个参数才是 include 请求的参数。

经过上面的启发,于是又想到了,是不是有别的AttributeName 可以仅取到 include 请求的参数呢?

通过测试,果然发现的确有该AttributeName:"javax.servlet.include.query_string"

只要通过一句话便可以得到该? 参数字符串:

  1. (String)request.getAttribute("javax.servlet.include.query_string");
该结果即为:type2=include&param2=test2? 正是需要的仅为 include 请求的参数。

本文出自 http://1077267.blog.51cto.com/1067267/438224

?

热点排行