轉載自:http://listenup.iteye.com/blog/1559553
1.Filter過濾器只過濾jsp文件不過濾action請求解決方案
解決辦法:在web.xml中將filter的配置放在struts2配置的前面。
2.攔截器與Filter的區別
Spring的攔截器與Servlet的Filter有相似之處,比如二者都是AOP編程思想的體現,都能實現權限檢查、日志記錄等。不同的是:
使用范圍不同:Filter是Servlet規范規定的,只能用於Web程序中。而攔截器既可以用於Web程序,也可以用於Application、Swing程序中。
規范不同:Filter是在Servlet規范中定義的,是Servlet容器支持的。而攔截器是在Spring容器內的,是Spring框架支持的。
使用的資源不同:同其他的代碼塊一樣,攔截器也是一個Spring的組件,歸Spring管理,配置在Spring文件中,因此能使用Spring里的任何資源、對象,例如Service對象、數據源、事務管理等,通過IoC注入到攔截器即可;而Filter則不能。
深度不同:Filter在只在Servlet前后起作用。而攔截器能夠深入到方法前后、異常拋出前后等,因此攔截器的使用具有更大的彈性。所以在Spring構架的程序中,要優先使用攔截器。
實際上Filter和Servlet極其相似,區別只是Filter不能直接對用戶生成響應。實際上Filter里doFilter()方法里的代碼就是從多個Servlet的service()方法里抽取的通用代碼,通過使用Filter可以實現更好的復用。
filter是一個可以復用的代碼片段,可以用來轉換HTTP請求、響應和頭信息。Filter不像Servlet,它不能產生一個請求或者響 應,它只是修改對某一資源的請求,或者修改從某一的響應。
JSR中說明的是,按照多個匹配的Filter,是按照其在web.xml中配置的順序 來執行的。
所以這也就是,把自己的Filter或者其他的Filter(比如UrlRewrite的Filter)放在Struts的 DispatcherFilter的前面的原因。因為,它們需要在請求被Struts2框架處理之前,做一些前置的工作。
當Filter被調用,並且進入了Struts2的DispatcherFilter中 后,Struts2會按照在Action中配置的Interceptor Stack中的Interceptor的順序,來調用Interceptor。
參考自:http://www.cnblogs.com/Fskjb/archive/2010/03/27/1698448.html
3.servlet、filter、interceptor的執行順序
Filter代碼:
- @Override
- public void doFilter(ServletRequest servletrequest,
- ServletResponse servletresponse, FilterChain filterchain)
- throws IOException, ServletException {
- System.out.println("in filter 1.");
- filterchain.doFilter(servletrequest, servletresponse);
- System.out.println("outing filter 1");
- }
interceptor代碼:
@Override
- public String intercept(ActionInvocation actioninvocation) throws Exception {
- System.out.println("in logininterceptor");
- String result=actioninvocation.invoke();
- System.out.println("outing logininterceptor");
- return result;
- }
action代碼:
- @Override
- public String execute() throws Exception {
- System.out.println("in loginaciton");
- ActionContext context=ActionContext.getContext();
- Map<String, Object> session=context.getSession();
- session.put("userName", userName);
- /* HttpServletRequest request = ServletActionContext. getRequest();
- HttpSession session = request.getSession();
- session.putValue("userName", userName);*/
- System.out.println("outing loginaciton");
- return SUCCESS;
- }
jsp代碼:
- <script type="text/javascript">
- function submitForm(){
- document.getElementById("form1").submit();
- }
- </script>
- </head>
- <body>
- This is Login page. <br>
- <form action="<%=path %>/login2.action" method="post" id="form1" name="form1">
- UserName:<input type="text" id="userName" name="userName"/><input type="button" value="submit" onclick="submitForm()" id="submit1" />
- </form>
- </body>
struts.xml
- <struts>
- <package name="default" extends="struts-default" namespace="/">
- <interceptors>
- <interceptor name="MyInterceptor" class="Login.LoginInterceptor"></interceptor>
- <interceptor-stack name="myInterceptorStack">
- <interceptor-ref name="MyInterceptor"/>
- <interceptor-ref name="defaultStack"/>
- </interceptor-stack>
- </interceptors>
- <action name="login2" class="Login.LoginAction">
- <result name="success">
- /Login/success.jsp
- </result>
- <interceptor-ref name="myInterceptorStack"></interceptor-ref>
- </action>
- </package>
- </struts>
console:
in filter 1.
in logininterceptor
in loginaciton
outing loginaciton
outing logininterceptor
outing filter 1
3.servlet、filter的執行順序
servlet代碼:
- public void init() throws ServletException {
- System.out.println("servlet初始化");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("in servlet");
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out
- .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
- out.println("<HTML>");
- out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
- out.println(" <BODY>");
- out.print(" This is ");
- out.print(this.getClass());
- out.println(", using the POST method");
- out.println("<br>");
- String x = request.getParameter("x");
- String y = request.getParameter("y");
- out.println("x="+x);
- out.println("<br>");
- out.println("y="+y);
- out.println("<br>");
- out.println(" </BODY>");
- out.println("</HTML>");
- out.flush();
- out.close();
- System.out.println("outing servlet");
- }
- public void destroy(){
- System.out.println("servlet銷毀");
- super.destroy();
- }
console:
servlet初始化
in filter 1.
in servlet
before HttpServletRequest
after HttpServletRequest
outing servlet
outing filter 1
當tomcat容器停止的時候,輸出:servlet銷毀