Web监听器导图详解

  监听器是JAVA Web开发中很重要的内容,其中波及到的常识,能够参考上面导图:

一、Web监听器

  1. 什么是web监听器?

==============

  web监听器是一种Servlet中的非凡的类,它们能帮忙开发者监听web中的特定事件,比方ServletContext,HttpSession,ServletRequest的创立和销毁;变量的创立、销毁和批改等。能够在某些动作前后减少解决,实现监控。

  1. 监听器罕用的用处

============

  通常应用Web监听器做以下的内容:

  统计在线人数,利用HttpSessionLisener

  加载初始化信息:利用ServletContextListener

  统计网站访问量

  实现拜访监控

  1. 接下来看看一个监听器的创立以及执行过程

=======================

   首先须要创立一个监听器,实现某种接口,例如我想实现一个对在线人数的监控,能够创立如下的监听器:

public class MyListener implements HttpSessionListener{    private int userNumber = 0;    public void sessionCreated(HttpSessionEvent arg0) {        userNumber++;        arg0.getSession().setAttribute("userNumber", userNumber);    }    public void sessionDestroyed(HttpSessionEvent arg0) {        userNumber--;        arg0.getSession().setAttribute("userNumber", userNumber);    }}

  而后在web.xml中配置该监听器,在web-app中增加:

 <listener>      <listener-class>com.test.MyListener</listener-class>  </listener>

  在JSP中增加拜访人数:

<body>    在线人数:<%=session.getAttribute("userNumber") %><br/></body>

  当我应用我的浏览器拜访时,执行后果如下:

  当关上另一个浏览器拜访时:

  因为关上另一个浏览器拜访,相当于另一个会话,因而在线人数会减少。

  对于3.0版本的Servlet来说,还反对应用注解的形式进行配置。

  那么接下来看看都有哪些监听器以及办法吧!

二、监听器的分类

  1. 依照监听的对象划分:

==============

  依照监听对象的不同能够划分为三种:

  ServletContext监控:对应监控application内置对象的创立和销毁。

  当web容器开启时,执行contextInitialized办法;当容器敞开或重启时,执行contextDestroyed办法。

  实现形式:间接实现ServletContextListener接口:

public class MyServletContextListener implements ServletContextListener{    public void contextDestroyed(ServletContextEvent sce) {    }    public void contextInitialized(ServletContextEvent sce) {    }}

  HttpSession监控:对应监控session内置对象的创立和销毁。

  当关上一个新的页面时,开启一个session会话,执行sessionCreated办法;当页面敞开session过期时,或者容器敞开销毁时,执行sessionDestroyed办法。

  实现形式:间接实现HttpSessionListener接口:

public class MyHttpSessionListener implements HttpSessionListener{    public void sessionCreated(HttpSessionEvent arg0) {    }    public void sessionDestroyed(HttpSessionEvent arg0) {    }}

  ServletRequest监控:对应监控request内置对象的创立和销毁。

  当拜访某个页面时,登程一个request申请,执行requestInitialized办法;当页面敞开时,执行requestDestroyed办法。

  实现形式,间接实现ServletRequestListener接口:

public class MyServletRequestListener implements ServletRequestListener{    public void requestDestroyed(ServletRequestEvent arg0) {    }    public void requestInitialized(ServletRequestEvent arg0) {    }}

  1. 依照监听事件划分:

=============

  2.1 监听事件本身的创立和销毁:同下面的按对象划分。

  2.2 监听属性的新增、删除和批改:

  监听属性的新增、删除和批改也是划分成三种,别离针对于ServletContext、HttpSession、ServletRequest对象:

  ServletContext,实现ServletContextAttributeListener接口:

  通过调用ServletContextAttribtueEvent的getName办法能够失去属性的名称。

public class MyServletContextAttrListener implements ServletContextAttributeListener {    public void attributeAdded( ServletContextAttributeEvent hsbe )    {        System.out.println( "In servletContext added :name = " + hsbe.getName() );    }    public void attributeRemoved( ServletContextAttributeEvent hsbe )    {        System.out.println( "In servletContext removed :name = " + hsbe.getName() );    }    public void attributeReplaced( ServletContextAttributeEvent hsbe )    {        System.out.println( "In servletContext replaced :name = " + hsbe.getName() );    }}

  HttpSession,实现HttpSessionAttributeListener接口:

public class MyHttpSessionAttrListener implements HttpSessionAttributeListener {    public void attributeAdded( HttpSessionBindingEvent hsbe )    {        System.out.println( "In httpsession added:name = " + hsbe.getName() );    }    public void attributeRemoved( HttpSessionBindingEvent hsbe )    {        System.out.println( "In httpsession removed:name = " + hsbe.getName() );    }    public void attributeReplaced( HttpSessionBindingEvent hsbe )    {        System.out.println( "In httpsession replaced:name = " + hsbe.getName() );    }}

  ServletRequest,实现ServletRequestAttributeListener接口:

public class MyServletRequestAttrListener implements ServletRequestAttributeListener {    public void attributeAdded( ServletRequestAttributeEvent hsbe )    {        System.out.println( "In servletrequest added :name = " + hsbe.getName() );    }    public void attributeRemoved( ServletRequestAttributeEvent hsbe )    {        System.out.println( "In servletrequest removed :name = " + hsbe.getName() );    }    public void attributeReplaced( ServletRequestAttributeEvent hsbe )    {        System.out.println( "In servletrequest replaced :name = " + hsbe.getName() );    }}

  2.3 监听对象的状态:

  针对某些POJO类,能够通过实现HttpSessionBindingListener接口,监听POJO类对象的事件。例如:

public class User implements HttpSessionBindingListener,Serializable{    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public void valueBound(HttpSessionBindingEvent hsbe) {        System.out.println("valueBound name: "+hsbe.getName());    }    public void valueUnbound(HttpSessionBindingEvent hsbe) {        System.out.println("valueUnbound name: "+hsbe.getName());    }}

  Session数据的钝化与活化:

  因为session中保留大量拜访网站相干的重要信息,因而过多的session数据就会服务器性能的降落,占用过多的内存。因而相似数据库对象的长久化,web容器也会把不常应用的session数据长久化到本地文件或者数据中。这些都是有web容器本人实现,不须要用户设定。

  不必的session数据序列化到本地文件中的过程,就是钝化;

  当再次拜访须要到该session的内容时,就会读取本地文件,再次放入内存中,这个过程就是活化。

  相似的,只有实现HttpSeesionActivationListener接口就是实现钝化与活化事件的监听:

public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable{    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public void valueBound(HttpSessionBindingEvent hsbe) {        System.out.println("valueBound name: "+hsbe.getName());    }    public void valueUnbound(HttpSessionBindingEvent hsbe) {        System.out.println("valueUnbound name: "+hsbe.getName());    }    public void sessionDidActivate(HttpSessionEvent hsbe) {        System.out.println("sessionDidActivate name: "+hsbe.getSource());    }    public void sessionWillPassivate(HttpSessionEvent hsbe) {        System.out.println("sessionWillPassivate name: "+hsbe.getSource());    }}

三、Servlet版本与Tomcat版本

  首先看一下Tomcat官网给出的匹配:

  如果版本不匹配,那么tomcat是不能公布该工程的,首先看一下版本不匹配时,会产生什么!

  我试图创立一个web工程,并且选取了Servlet3.0版本:

  而后我想要在tomcat6中公布,能够看到报错了!

  JDK版本不对....这是在平时开发如果对Servlet不相熟的web老手,常犯的谬误。

解决办法:

  1 在创立时,间接公布到Tomcat容器中,此时Servlet仅仅会列出Tomcat反对的版本:

  2 批改工程Servlet版本配置信息,文件为:工作目录SessionExample.settingsorg.eclipse.wst.common.project.facet.core.xml

<?xml version="1.0" encoding="UTF-8"?><faceted-project>  <runtime name="Apache Tomcat v6.0"/>  <fixed facet="java"/>  <fixed facet="wst.jsdt.web"/>  <fixed facet="jst.web"/>  <installed facet="java" version="1.7"/>  <installed facet="jst.web" version="2.5"/>  <installed facet="wst.jsdt.web" version="1.0"/></faceted-project>

四、getAttribute与getParameter的区别

  这部分是对JSP的扩大,常常在JSP或者Servlet中获取数据,那么getAttribute与getParameter有什么区别呢?

  1. 从获取到数据的起源来说:

================

  getAttribtue获取到的是web容器中的值,比方:

  咱们在Servlet中通过setAttribute设定某个值,这个值存在于容器中,就能够通过getAttribute办法获取;

  getParameter获取到的是通过http传来的值,比方这样一个http申请:

http:localhost:8080/test/test.html?username=xingoo

  还有其余的GET和POST形式,都能够通过getParameter来获取。

  1. 从获取到的数据类型来说:

================

  getAttribute返回的是一个对象,Object。

  getParameter返回的是,后面页面中某个表单或者http前面参数传递的值,是个字符串。

原文:https://juejin.cn/post/691197...

举荐浏览

=====

程序员年薪百万的飞马打算你据说过吗?

为什么阿里巴巴的程序员成长速度这么快?

从事开发一年的程序员能拿到多少钱?

字节跳动总结的设计模式 PDF 火了,完整版凋谢下载

对于【暴力递归算法】你所不晓得的思路

开拓鸿蒙,谁做零碎,聊聊华为微内核