JDBC分页/监听器

JDBC分页/监听器

 

分页

l  物理分页

  • 在sql查询时,从数据库只检索分页需要的数据
  • 通常不同的数据库有着不同的物理分页语句
  • mysql物理分页,采用limit关键字
  • 例如:检索11-20条 select * from user limit 10,10 ;

l  逻辑分页

  • 在sql查询时,先从数据库检索出所有数据的结果集
  • 在程序内,通过逻辑语句获得分页需要的的数据
  • 例如: 检索11-20条 userList.subList(10,20);

条件查询

l  条件查询 指对数据的查询结果设计条件

  • 一般条件 可以由用户选择

使用模糊查询like

监听器

l  监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

Servlet监听器

l  在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象。

 

l  Servlet规范针对这三个对象上的操作,又把这多种类型的监听器划分为三种类型。

  • 监听三个域对象创建和销毁的事件监听器
  • 监听域对象中属性的增加和删除的事件监听器
  • 监听绑定到 HttpSession 域中的某个对象的状态的事件监听器。(查看API文档)

编写监听器

l  和编写其它事件监听器一样,编写servlet监听器也需要实现一个特定的接口,并针对相应动作覆盖接口中的相应方法。

l  和其它事件监听器略有不同的是,servlet监听器的注册不是直接注册在事件源上,而是由WEB容器负责注册,开发人员只需在web.xml文件中使用<listener>标签配置好监听器,web容器就会自动把监听器注册到事件源中。

l  一个 web.xml 文件中可以配置多个 Servlet 事件监听器,web 服务器按照它们在 web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器。

 

ServletContext监听器

l  ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。

l  当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法

l  当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。

 

l  提问,servletContext域对象何时创建和销毁:

  • 创建:服务器启动针对每一个web应用创建servletcontext
  • 销毁:服务器关闭前先关闭代表每一个web应用的servletContext

 

示例1 简单任务调度Timer与TimerTask使用

HttpSession监听器

l  HttpSessionListener接口用于监听HttpSession的创建和销毁

l  创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。

l  销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。

 

l  (此处复习session对象,写多个servlet都去getSession,看session的创建)

l  Session域对象创建和销毁的时机创建:用户每一次访问时,服务器创建session

  • 销毁:如果用户的session 30分钟没有使用,服务器就会销毁session,我们在web.xml里面也可以配置session失效时间

 

ServletRequest 监听器

l  ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。

l  Request 对象被创建时,监听器的requestInitialized方法将会被调用。

l  Request对象被销毁时,监听器的requestDestroyed方法将会被调用。

 

 

l  (此处复习request对象,在浏览器窗口中多次刷新访问servlet,看request对象的创建和销毁,并写一个servlet,然后用sendRedirect、forward方式跳转到其它servlet,查看request对象的创建和消耗)

l  servletRequest域对象创建和销毁的时机:

  • 创建:用户每一次访问,都会创建一个reqeust
  • 销毁:当前访问结束,request对象就会销毁

 

l  当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行相应,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

 

l  各个域属性监听器中的完整语法定义为:

  • public void attributeAdded(ServletContextAttributeEvent scae)
  • public void attributeAdded (HttpSessionBindingEvent  hsbe)
  • public void attributeAdded(ServletRequestAttributeEvent srae)

 

监听三个域的属性变化

l  Servlet规范定义了监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。

l  这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener ServletRequestAttributeListener

l  这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。

 

l  当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行相应,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

 

l  各个域属性监听器中的完整语法定义为:

  • public void attributeAdded(ServletContextAttributeEvent scae)
  • public void attributeAdded (HttpSessionBindingEvent  hsbe)
  • public void attributeAdded(ServletRequestAttributeEvent srae)

 

l  当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应

l  各个域属性监听器中的完整语法定义为:

  • public void attributeRemoved(ServletContextAttributeEvent scae)
  • public void attributeRemoved (HttpSessionBindingEvent  hsbe)
  • public void attributeRemoved (ServletRequestAttributeEvent srae)

 

l  当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行相应

l  各个域属性监听器中的完整语法定义为:

  • public void attributeReplaced(ServletContextAttributeEvent scae)
  • public void attributeReplaced (HttpSessionBindingEvent  hsbe)
  • public void attributeReplaced (ServletRequestAttributeEvent srae)

Session绑定监听器

l  保存在 Session 域中的对象可以有多种状态:绑定到  Session 中;从 Session 域中解除绑定;随 Session 对象持久化到一个存储设备中(钝化);随 Session 对象从一个存储设备中恢复(活化)

l  Servlet 规范中定义了两个特殊的监听器接口来帮助 JavaBean 对象了解自己在 Session 域中的这些状态:HttpSessionBindingListener接口和HttpSessionActivationListener接口 ,实现这两个接口的类不需要 web.xml 文件中进行注册

 

l  实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件

l  当对象被绑定到 HttpSession 对象中时,web 服务器调用该对象的  void valueBound(HttpSessionBindingEvent event) 方法

l  当对象从 HttpSession 对象中解除绑定时,web 服务器调用该对象的 void valueUnbound(HttpSessionBindingEvent event)方法

 

 

l  实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件

l  当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被钝化之前,web 服务器调用如下方法sessionWillPassivate(HttpSessionBindingEvent event) 方法

l  当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被活化之后,web 服务器调用该对象的 void sessionDidActive(HttpSessionBindingEvent event)方法

 

<Context>

<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">

<Store className="org.apache.catalina.session.FileStore" directory="it315"/>

</Manager>

</Context>

 

posted @ 2017-11-12 11:19  Nevada  阅读(197)  评论(0)    收藏  举报