[转帖]filter的使用_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 4534 | 回复: 0   主题: [转帖]filter的使用        下一篇 
xuefeng
注册用户
等级:上士
经验:315
发帖:69
精华:0
注册:2011-8-17
状态:离线
发送短消息息给xuefeng 加好友    发送短消息息给xuefeng 发消息
发表于: IP:您无权察看 2011-8-29 12:37:34 | [全部帖] [楼主帖] 楼主

     struts2与先前的struts不同之一就是:struts使用servlet类来处理前台消息,而struts2使用filter类来处理前台消息,有很多人喜欢把filter也称为一种特殊的servlet,因为它确实在struts2中所实现的功能几乎和servlet一个样子刻出来的,但其终究还不是完全的servlet,否则也不会叫filter了。filter与servlet最大的不同之处就在于:filter只能进行消息的预处理(可以是前台传来的消息,或者是servlet向前台进行输出的消息),却不能直接向用户生成响应;一个filter可同时处理多个用户请求,而一个servlet只能处理单个请求。filter的生命周期和servlet大体相同:

1.void init():初始化filter;

2.void destroy():销毁filter前,回收某些资源;

3.void doFilter():filter的实现主体;

filter的配置方式也和servlet差不多,但要注意的是,servlet由于只能处理单个请求,所以其url不能是匹配型的,而filter可处理多个请求,其Url既可以是匹配型的,也可以是指定的单个请求。如下所示:

  1. <servlet>
  2. <servlet-name>myServlet</servlet-name>
  3. <servlet-class>com.servlet.LoginServlet</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>myServlet</servlet-name>
  7. <url-pattern>/login</url-pattern>
  8. </servlet-mapping>
  9. <filter>
  10. <filter-name>myFilter</filter-name>
  11. <filter-class>com.filter.FirstFilter</filter-class>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>myFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

<servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <filter> <filter-name>myFilter</filter-name> <filter-class>com.filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>


注意看url-pattrn的区别。如果在servlet-mapping的url-pattern配置为/*,则会出现运行错误。

我们来看一个简单登陆案例:在客户端输入用户名和密码时,filter负责获取客户端IP地址和定位消息处理URL,servlet负责验证登陆信息是否正确。

index.jsp关键代码如下(两个输入框和一个提交按钮):

  1. <body>
  2. <formaction="login.do"id = "login"method="get">
  3.         user name:<inputtype="text"name="username"><br/>
  4.         user password:<inputtype = "text"name="userpassword"><br/>
  5. <inputtype = "submit"value = "login">
  6. </form>
  7. </body>

<body> <form action="login.do" id = "login" method="get"> user name:<input type="text" name="username"><br/> user password:<input type = "text" name="userpassword"><br/> <input type = "submit" value = "login"> </form> </body>


success.jsp代码如下:

  1. success!  

success!


error.jsp代码如下:

  1. <body>
  2. <%=request.getAttribute("err") %><br/>
  3.     login failed!  
  4. </body>

<body> <%=request.getAttribute("err") %><br/> login failed! </body>


FirstFilter类代码如下(向控制台输出客户端IP地址和消息处理URL):

  1. package com.filter; 
  2. import java.io.IOException; 
  3. import javax.servlet.Filter; 
  4. import javax.servlet.FilterChain; 
  5. import javax.servlet.FilterConfig; 
  6. import javax.servlet.RequestDispatcher; 
  7. import javax.servlet.ServletException; 
  8. import javax.servlet.ServletRequest; 
  9. import javax.servlet.ServletResponse; 
  10. import javax.servlet.http.HttpServletRequest; 
  11. import javax.servlet.http.HttpServletResponse; 
  12. import javax.servlet.http.HttpSession; 
  13. publicclass FirstFilter implements Filter{ 
  14.       
  15.       private FilterConfig config; 
  16.       publicvoid destroy() { 
  17.              config = null; 
  18.        } 
  19.       
  20.       publicvoid doFilter(ServletRequest request, ServletResponse response, 
  21.        FilterChain chain) throws IOException, ServletException { 
  22.             // TODO Auto-generated method stub 
  23.              HttpServletRequest httpRequest = (HttpServletRequest)request; 
  24.              HttpServletResponse httpResponse = (HttpServletResponse)response; 
  25.             long before = System.currentTimeMillis(); 
  26.              System.out.println("开始过滤。。。"); 
  27.              System.out.println("拦截到用户IP地址:"+ httpRequest.getRemoteAddr()); 
  28.              chain.doFilter(request, response);//过滤器不进行消息处理,消息处理继续传递到servlet中进行响应 
  29.             long after = System.currentTimeMillis(); 
  30.              System.out.println("过滤结束!过滤所花时间为:"+ (after-before) ); 
  31.              System.out.println("请求被定为到:" + httpRequest.getRequestURL()); 
  32.             // if("James".equals(request.getAttribute("username") ) && "admin".equals(request.getAttribute("userpassword"))) 
  33.             // { 
  34.                   // RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp"); 
  35.                   // dispatcher.forward(request, response); 
  36.             // } 
  37.             // else 
  38.             // { 
  39.                   // RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp"); 
  40.                   // dispatcher.forward(request, response); 
  41.             // } 
  42.        } 
  43.       
  44.       publicvoid init(FilterConfig config) throws ServletException { 
  45.             this.config = config; 
  46.        } 

package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class FirstFilter implements Filter{ private FilterConfig config; public void destroy() { config = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse)response; long before = System.currentTimeMillis(); System.out.println("开始过滤。。。"); System.out.println("拦截到用户IP地址:"+ httpRequest.getRemoteAddr()); chain.doFilter(request, response);//过滤器不进行消息处理,消息处理继续传递到servlet中进行响应 long after = System.currentTimeMillis(); System.out.println("过滤结束!过滤所花时间为:"+ (after-before) ); System.out.println("请求被定为到:" + httpRequest.getRequestURL()); // if("James".equals(request.getAttribute("username") ) && "admin".equals(request.getAttribute("userpassword"))) // { // RequestDispatcher dispatcher = request.getRequestDispatcher("/success.jsp"); // dispatcher.forward(request, response); // } // else // { // RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp"); // dispatcher.forward(request, response); // } } public void init(FilterConfig config) throws ServletException { this.config = config; } }


LoginServlet类代码如下(对登陆信息进行验证):

  1. package com.servlet; 
  2. import java.io.IOException; 
  3. import javax.servlet.RequestDispatcher; 
  4. import javax.servlet.ServletException; 
  5. import javax.servlet.http.HttpServlet; 
  6. import javax.servlet.http.HttpServletRequest; 
  7. import javax.servlet.http.HttpServletResponse; 
  8. publicclass LoginServlet extends HttpServlet{ 
  9.       private String userpassword; 
  10.       private String username; 
  11.       publicvoid service(HttpServletRequest request,HttpServletResponse response) 
  12.        { 
  13.              RequestDispatcher dispatcher; 
  14.              username = request.getParameter("username"); 
  15.              userpassword = request.getParameter("userpassword"); 
  16.             
  17.             if("James".equals(username)&& "admin".equals(userpassword)) 
  18.              { 
  19.                    dispatcher = request.getRequestDispatcher("/success.jsp"); 
  20.                   try { 
  21.                          dispatcher.forward(request, response); 
  22.                    } catch (ServletException e) { 
  23.                         // TODO Auto-generated catch block 
  24.                          e.printStackTrace(); 
  25.                    } catch (IOException e) { 
  26.                         // TODO Auto-generated catch block 
  27.                          e.printStackTrace(); 
  28.                    } 
  29.              } 
  30.             else
  31.              { 
  32.                    dispatcher = request.getRequestDispatcher("/error.jsp"); 
  33.                   try { 
  34.                          dispatcher.forward(request, response); 
  35.                    } catch (ServletException e) { 
  36.                         // TODO Auto-generated catch block 
  37.                          e.printStackTrace(); 
  38.                    } catch (IOException e) { 
  39.                         // TODO Auto-generated catch block 
  40.                          e.printStackTrace(); 
  41.                    } 
  42.              } 
  43.        } 

package com.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet{ private String userpassword; private String username; public void service(HttpServletRequest request,HttpServletResponse response) { RequestDispatcher dispatcher; username = request.getParameter("username"); userpassword = request.getParameter("userpassword"); if("James".equals(username)&& "admin".equals(userpassword)) { dispatcher = request.getRequestDispatcher("/success.jsp"); try { dispatcher.forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { dispatcher = request.getRequestDispatcher("/error.jsp"); try { dispatcher.forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }


web.xml配置文件如下:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <servlet>
  11. <servlet-name>myServlet</servlet-name>
  12. <servlet-class>com.servlet.LoginServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>myServlet</servlet-name>
  16. <url-pattern>/login.do</url-pattern>
  17. </servlet-mapping>
  18. <filter>
  19. <filter-name>myFilter</filter-name>
  20. <filter-class>com.filter.FirstFilter</filter-class>
  21. </filter>
  22. <filter-mapping>
  23. <filter-name>myFilter</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
  26. </web-app>

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping> <filter> <filter-name>myFilter</filter-name> <filter-class>com.filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>


在控制台中输出的结果为:

开始过滤。。。
拦截到用户IP地址:10.0.0.121
过滤结束!过滤所花时间为:625
请求被定为到:http://pc-201101141739:8080/try/  或 请求被定为到:http://pc-201101141739:8080/try/login.do

另外需要特别注意的一点是:servlet中可以进行的消息处理行为(向前台输出内容除外),在filter中也可以进行处理。为了进行验证,我们可以把上面filter代码中注释部分去掉,则网页一开始就会显示为:

terrible!
login failed!


这就是说,在这里,我们完全可以不用servlet,而只单用一个filter就能进行所有的消息处理。这也是struts2的消息处理机制:struts2的配置文件中没有servlet类的配置,只有一个filter的配置,由此可以看出其用filter处理所有的消息,至于如何处理前台传来的各种消息,我会在下一篇文章中详细阐述。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论