Listener Servlet的应用
Listener是 Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:
ServletContextAttributeListener监听对 ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener监听 ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听 HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听 HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。
要获得以上的功能,监听器必须实现以下3个接口:
下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。
要获得以上的功能,监听器必须实现以下3个接口:
HttpSessionListener
ServletContextListener
ServletContextAttributeListener
我们看具体的代码,见示例 14-9。
【程序源代码】
1 // ==================== Program Discription ===================== 2 // 程序名称:示例 14-9 : EncodingFilter .java 3 // 程序目的:学习使用监听器 4 // ============================================================== 5 import javax.servlet.http.*; 6 import javax.servlet.*; 7 8 public class OnLineCountListener implements HttpSessionListener, ServletContextListener,ServletContextAttributeListener 9 { 10 private int count; 11 private ServletContext context = null; 12 13 public OnLineCountListener() 14 { 15 count=0; 16 //setContext(); 17 } 18 //创建一个 session时激发 19 public void sessionCreated(HttpSessionEvent se) 20 { 21 count++; 22 setContext(se); 23 24 } 25 //当一个 session失效时激发 26 public void sessionDestroyed(HttpSessionEvent se) 27 { 28 count--; 29 setContext(se); 30 } 31 //设置 context的属性,它将激发attributeReplaced或attributeAdded方法 32 public void setContext(HttpSessionEvent se) 33 { 34 se.getSession().getServletContext(). setAttribute("onLine",new Integer(count)); 35 } 36 //增加一个新的属性时激发 37 public void attributeAdded(ServletContextAttributeEvent event) { 38 39 log("attributeAdded('" + event.getName() + "', '" + 40 event.getValue() + "')"); 41 42 } 43 44 //删除一个新的属性时激发 45 public void attributeRemoved(ServletContextAttributeEvent event) { 46 47 log("attributeRemoved('" + event.getName() + "', '" + 48 event.getValue() + "')"); 49 50 } 51 52 //属性被替代时激发 53 public void attributeReplaced(ServletContextAttributeEvent event) { 54 55 log("attributeReplaced('" + event.getName() + "', '" + 56 event.getValue() + "')"); 57 } 58 //context删除时激发 59 public void contextDestroyed(ServletContextEvent event) { 60 61 log("contextDestroyed()"); 62 this.context = null; 63 64 } 65 66 //context初始化时激发 67 public void contextInitialized(ServletContextEvent event) { 68 69 this.context = event.getServletContext(); 70 log("contextInitialized()"); 71 72 } 73 private void log(String message) { 74 75 System.out.println("ContextListener: " + message); 76 } 77 } |
【程序注解】
在OnLineCountListener里,用count代表当前在线的人数,OnLineCountListener将在Web服务器启动时自动执行。当OnLineCountListener构造好后,把count设置为0。每增加一个Session,OnLineCountListener会自动调用sessionCreated(HttpSessionEvent se)方法;每销毁一个Session,OnLineCountListener会自动调用sessionDestroyed(HttpSessionEvent se)方法。当调用sessionCreated(HttpSessionEvent se)方法时,说明又有一个客户在请求,此时使在线的人数(count)加1,并且把count写到ServletContext中。ServletContext的信息是所有客户端共享的,这样,每个客户端都可以读取到当前在线的人数。
为了使监听器生效,需要在web.xml里进行配置,如下所示:
测试程序:
<%&#64; page contentType&#61;"text/html;charset&#61;gb2312" %> |
目前在线人数&#xff1a;
<%&#61;getServletContext().getAttribute("onLine")%> |
退出会话&#xff1a;
getServletContext().getAttribute("onLine")获得了 count的具体值。客户端调用
<%session.invalidate() ;%> |
使 Session失效&#xff0c;这样监听器就会使count减1。
【运行程序】
web.xml做好以上的配置&#xff0c;把OnLineCountListener放在WEB-INF/class目录下&#xff0c;启动Web服务器&#xff0c;在浏览器里输入以下URL&#xff08;根据具体情况不同&#xff09;&#xff1a;http://127.0.0.1:8080/ch14/listener.jsp
浏览器将会打印目前在线人数。在服务器端有以下输出&#xff1a;
… ContextListener: contextInitialized() ContextListener: attributeReplaced(&#39;org.apache. catalina.WELCOME_FILES&#39;, &#39;[Ljava.lang.String;&#64;1d98a&#39;) … ContextListener: attributeAdded(&#39;onLine&#39;, &#39;1&#39;) ContextListener: attributeReplaced(&#39;onLine&#39;, &#39;1&#39;) ContextListener: attributeReplaced(&#39;onLine&#39;, &#39;0&#39;) ContextListener: attributeReplaced(&#39;onLine&#39;, &#39;1&#39;) ContextListener: attributeReplaced(&#39;onLine&#39;, &#39;2&#39;) |
用Servlet开发和配置过滤器
JSP技术构建在 Servlet技术之上&#xff0c;所以Servlet和JSP的技术本质是一样的&#xff0c;JSP能做到的&#xff0c;Servlet都能做到&#xff0c;但是它们却各有所长。Servlet比较适合作为控制类组件&#xff0c;比如视图控制器等。另外&#xff0c;Servlet还可以作为过滤器、监听器等。Servlet不仅可以动态生成HTML内容&#xff0c;还可以动态生成图形。总而言之&#xff0c;Servlet在项目中作为控制类的组件&#xff0c;并且处理一些后台业务&#xff0c;JSP则作为显示组件。
在本节&#xff0c;我们将介绍Servlet常用的使用方法之一&#xff1a;作为过滤器。在Servlet作为过滤器使用时&#xff0c;它可以对客户的请求进行过滤处理&#xff0c;当它处理完成后&#xff0c;它会交给下一个过滤器处理&#xff0c;就这样&#xff0c;客户的请求在过滤链里一个个处理&#xff0c;直到请求发送到目标。举个例子&#xff0c;某个网站里有提交"修改的注册信息"的网页&#xff0c;当用户填写完成修改信息并提交后&#xff0c;服务端在进行真正的处理时需要做两个处理&#xff1a;客户端的会话是否有效&#xff1b;对提交的数据进行统一的编码&#xff0c;比如GB2312。这两个处理可以在由两个过滤器组成的过滤链里进行处理。当过滤器处理成功后&#xff0c;把提交的数据发送到最终目标&#xff1b;如果过滤器处理不成功&#xff08;比如客户端的会话无效&#xff09;&#xff0c;它将把视图派发到指定的错误页面。可以看出&#xff0c;过滤器就像一扇门&#xff0c;客户端要和服务端的某个目标交互&#xff0c;必须通过这扇门。
下面我们来看一个具体的例子&#xff0c;这个例子将介绍怎么开发过滤器&#xff0c;并且介绍怎么在web.xml文件里配置过滤器。这个例子里有两个JSP页面&#xff0c;前一个页面用户输入一些信息然后提交&#xff0c;后一个页面显示用户提交的信息。在提交信息后&#xff0c;要经过两个过滤器的处理&#xff0c;一个检查用户是否登录&#xff0c;一个把用户的提交信息用GB2312进行重新编码。
开发一个Filter&#xff0c;这个Filter需要实现Filter接口&#xff0c;Filter接口定义了以下的方法&#xff1a;
在本节&#xff0c;我们将介绍Servlet常用的使用方法之一&#xff1a;作为过滤器。在Servlet作为过滤器使用时&#xff0c;它可以对客户的请求进行过滤处理&#xff0c;当它处理完成后&#xff0c;它会交给下一个过滤器处理&#xff0c;就这样&#xff0c;客户的请求在过滤链里一个个处理&#xff0c;直到请求发送到目标。举个例子&#xff0c;某个网站里有提交"修改的注册信息"的网页&#xff0c;当用户填写完成修改信息并提交后&#xff0c;服务端在进行真正的处理时需要做两个处理&#xff1a;客户端的会话是否有效&#xff1b;对提交的数据进行统一的编码&#xff0c;比如GB2312。这两个处理可以在由两个过滤器组成的过滤链里进行处理。当过滤器处理成功后&#xff0c;把提交的数据发送到最终目标&#xff1b;如果过滤器处理不成功&#xff08;比如客户端的会话无效&#xff09;&#xff0c;它将把视图派发到指定的错误页面。可以看出&#xff0c;过滤器就像一扇门&#xff0c;客户端要和服务端的某个目标交互&#xff0c;必须通过这扇门。
下面我们来看一个具体的例子&#xff0c;这个例子将介绍怎么开发过滤器&#xff0c;并且介绍怎么在web.xml文件里配置过滤器。这个例子里有两个JSP页面&#xff0c;前一个页面用户输入一些信息然后提交&#xff0c;后一个页面显示用户提交的信息。在提交信息后&#xff0c;要经过两个过滤器的处理&#xff0c;一个检查用户是否登录&#xff0c;一个把用户的提交信息用GB2312进行重新编码。
开发一个Filter&#xff0c;这个Filter需要实现Filter接口&#xff0c;Filter接口定义了以下的方法&#xff1a;
destroy() //由 Web容器调用&#xff0c;销毁此Filter init(FilterConfig filterConfig) ///由 Web容器调用&#xff0c;初始化此Filter doFilter(ServletRequest request, ServletResponse response, FilterChain chain)//具体过滤处理代码 |
下面我们来看对提交信息用 GB2312进行重新编码的Filter&#xff0c;见示例14-7、示例14-8。
【程序源代码】
1 // &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; Program Discription &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; 2 // 程序名称&#xff1a;示例 14-7 : EncodingFilter .java 3 // 程序目的&#xff1a;学习使用编码过滤器 4 // &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; 5 import javax.servlet.FilterChain; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import java.io.IOException; 9 import javax.servlet.Filter; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.ServletException; 13 import javax.servlet.FilterConfig; 14 15 public class EncodingFilter implements Filter 16 { 17 18 private String targetEncoding &#61; "gb2312"; 19 protected FilterConfig filterConfig; 20 21 public void init(FilterConfig config) throws ServletException { 22 this.filterConfig &#61; config; 23 this.targetEncoding &#61; config.getInitParameter("encoding"); 24 } 25 26 27 public void doFilter(ServletRequest srequest, ServletResponse sresponse,FilterChain chain) 28 throws IOException, ServletException { 29 30 HttpServletRequest request &#61; (HttpServletRequest)srequest; 31 request.setCharacterEncoding(targetEncoding);//把请求用指定的方式编码 32 // 把处理发送到下一个过滤器 33 chain.doFilter(srequest,sresponse); 34 } 35 36 public void destroy() 37 { 38 this.filterConfig&#61;null; 39 } 40 41 public void setFilterConfig(final FilterConfig filterConfig) 42 { 43 this.filterConfig&#61;filterConfig; 44 } 45 } |
【程序源代码】
1 // &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; Program Discription &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; 2 // 程序名称&#xff1a;示例 14-8 : LoginFilter.java 3 // 程序目的&#xff1a;学习使用登录过滤器 4 // &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; 5 import javax.servlet.FilterChain; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import java.io.IOException; 9 import javax.servlet.Filter; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.ServletException; 13 import javax.servlet.FilterConfig; 14 15 public class LoginFilter implements Filter 16 { 17 String LOGIN_PAGE&#61;"init.jsp"; 18 protected FilterConfig filterConfig; 19 public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException 20 { 21 HttpServletRequest hreq &#61; (HttpServletRequest)req; 22 HttpServletResponse hres &#61; (HttpServletResponse)res; 23 String isLog&#61;(String)hreq.getSession().getAttribute("isLog"); 24 if((isLog!&#61;null)&&((isLog.equals("true"))||(isLog&#61;&#61;"true")))//检查是否登录 25 { 26 chain.doFilter(req,res); 27 return ; 28 } 29 else 30 hres.sendRedirect(LOGIN_PAGE);//如果没有登录&#xff0c;把视图派发到登录页面 31 } 32 33 public void destroy() 34 { 35 this.filterConfig&#61;null; 36 } 37 public void init(FilterConfig config) 38 { 39 this.filterConfig&#61;config; 40 } 41 public void setFilterConfig(final FilterConfig filterConfig) 42 { 43 this.filterConfig&#61;filterConfig; 44 } 45 } |
【程序注解】
正如前面所说&#xff0c;EncodingFilter的目的是把客户端的请求用指定的方式编码&#xff0c;具体的处理在request.setCharacterEncoding(targetEncoding)完成了。LoginFilter判断用户在进入目标之前是否登录&#xff0c;if((isLog!&#61;null)&&((isLog.equals("true"))||(isLog&#61;&#61;"true")))将检查用户是否登录&#xff0c;如果已登录&#xff0c;那么把视图让过滤链继续处理&#xff0c;如果没有登录&#xff0c;把视图派发到登录页面&#xff0c;过滤链处理结束。
下面我们来看怎么在web.xml里配置这两个过滤器&#xff0c;代码如下所示&#xff1a;
【程序源代码】
【程序注解】
可以看出&#xff0c;配置Filter时&#xff0c;首先指定Filter的名字和Filter的实现类&#xff0c;如果有必要&#xff0c;还要配置Filter的初始参数&#xff1b;然后为Filter做映射&#xff0c;这个映射指定了需要过滤的目标&#xff08;JSP、Servlet&#xff09;。在上面的例子中&#xff0c;指定了EncodingFilter 为所有的JSP和Servlet做过滤&#xff0c;LoginFilter为target.jsp做过滤。这样&#xff0c;当客户请求target.jsp时&#xff0c;首先要经过EncodingFilter的处理&#xff0c;然后经过LoginFilter的处理&#xff0c;最后才把请求传递给target.jsp。
【运行程序】
把程序部署到Web服务器里&#xff08;比如Tomcat&#xff09;&#xff0c;然后启动Web服务器&#xff0c;在浏览器里输入以下URL&#xff08;根据具体请求改变URL&#xff09;&#xff1a;http://127.0.0.1:8080/ch14/target.jsp
那么Filter将会把视图派发到&#xff1a;http://127.0.0.1:8080/ch14/init.jsp
在init.jsp里&#xff0c;我们使用&#xff1a;
<% session.setAttribute("isLog","true");%> |
来设置用户已经登录&#xff08;这里是简化的&#xff0c;在实际项目中&#xff0c;可能要经过验证处理&#xff09;。在 init.jsp里&#xff0c;可以提交一些中文的信息。由于提交的信息被EncodingFilter使用GB2312统一编码了&#xff0c;故在target.jsp里能够正确显示中文。您可以做一个试验&#xff0c;把
改为
然后重新启动 Web服务器。那么在target.jsp里&#xff0c;中文将不能正确显示。
原文链接&#xff1a;http://www.dlog.cn/nicholascoder/diary/9282