JavaWeb三大组件

来源:互联网 发布:java nio attach 编辑:程序博客网 时间:2024/05/14 06:31

Servlet

Listener

事件源:(前三个为 JavaWeb三大域)!

ServletContext

  • 生命周期监听:ServletContextListener,它有两个方法,分别在服务器启动和关闭时调用
    • void contextInitialized(ServletContextEvent sce)
    • void contextDestroyed(ServletContextEvent sce)
  • 属性监听:ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
    • void attributeAdded(ServletContextAttributeEvent event)
    • void attributeReplaced(ServletContextAttributeEvent event)
    • void attributeRemoved(ServletContextAttributeEvent event)
  • 功能介绍:
    ServletContextAttributeEvent
    • String getName():获取当前操作的属性名;
    • Object getValue():获取当前操作的属性值;
    • ServletContext getServletContext():获取ServletContext对象。

HttpSession

  • 生命周期监听:HttpSessionListener,它有两个方法,分别在开始会话和结束会话时调用
    • void sessionCreated(HttpSessionEvent se)
    • void sessionDestroyed(HttpSessionEvent se)
  • 属性监听:HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
    • void attributeAdded(HttpSessionBindingEvent event)
    • void attributeReplaced(HttpSessionBindingEvent event)
    • void attributeRemoved(HttpSessionBindingEvent event)
  • 功能介绍:
    HttpSessionBindingEvent
    • String getName():获取当前操作的属性名;
    • Object getValue():获取当前操作的属性值;
    • HttpSession getSession():获取当前操作的session对象。

ServletRequest


  • 生命周期监听:ServletRequestListener,它有两个方法,分别在开始请求和结束请求时调用.
    • void requestInitialized(ServletRequestEvent sre)
    • void requestDestroyed(ServletRequestEvent sre)
  • 属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
    • void attributeAdded(ServletRequestAttributeEvent srae)
    • void attributeReplaced(ServletRequestAttributeEvent srae)
    • void attributeRemoved(ServletRequestAttributeEvent srae)
  • 功能介绍
    ServletRequestAttributeEvent
    • String getName():获取当前操作的属性名;
    • Object getValue():获取当前操作的属性值;
    • ServletContext getServletContext():获取ServletContext对象;
    • ServletRequest getServletRequest():获取当前操作的ServletRequest对象。

  • javaWeb中完成编写监听器,必须去实现某个监听器接口并且在web.xml中配置来完成注册!

感知监听(都与HttpSession相关)用来添加到JavaBean上,而不是添加到三大域上,这里一共两个监听器,都不需要在web.xml中注册!

HttpSessionBindingListener

  • 当某个类实现了该接口后,可以感知本类对象添加到session中,以及感知从session中移除
  • public void valueBound(HttpSessionBindingEvent event):当把监听器对象添加到session中会调用监听器对象的本方法;
  • public void valueUnbound(HttpSessionBindingEvent event):当把监听器对象从session中移除时会调用监听器对象的本方法;

HttpSessionActivationListener

  • public void sessionWillPassivate(HttpSessionEvent se):当对象感知被活化时调用本方法;
  • public void sessionDidActivate(HttpSessionEvent se):当对象感知被钝化时调用本方法;
    注意,因为钝化和活化session,其实就是使用序列化和反序列化技术把session从内存保存到硬盘,和把session从硬盘加载到内存。这说明如果该类没有实现Serializable接口,那么当session钝化时就不会钝化该对象,而是把该对象从session中移除再钝化!这也说明session活化后,session中就不在有该类对象了。
  • 示例步骤:
    配置Tomcat钝化session的参数,把下面配置文件放到tomcat\conf\catalina\localhost目录下!文件名称为项目名称。
<Context><ManagerclassName="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"[如果session在1分钟内没有使用,那么Tomcat就会钝化它]><Store className="org.apache.catalina.session.FileStore" directory="mysession"[把session序列化到Tomcat\work\Catalina\localhost\listener\mysession目录下。]/>    </Manager></Context>

事件对象

  • ServletContextEvent:ServletContext getServletContext();
  • HttpSeessionEvent:HttpSession getSession();
  • ServletRequestEvent:
    • ServletRequest getServletRequest()
    • ServletContext getServletContext()

语言问题

public class Demo1 {    public void fun1() {        ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("zh", "CN" ));        String username = rb.getString("msg.username");        String password = rb.getString("msg.password");        System.out.println(username);        System.out.println(password);           }    public void fun2() {        ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("en", "US" ));        String username = rb.getString("msg.username");        String password = rb.getString("msg.password");        System.out.println(username);        System.out.println(password);           }}`

Filter

  • 过滤器的生命周期
    • init(FilterConfig):在服务器启动时会创建Filter实例,并且每个类型的Filter只创建一个实例,从此不再创建!在创建完Filter实例后,会马上调用init()方法完成初始化工作,这个方法只会被执行一次;
    • doFilter(ServletRequest req,ServletResponse res,FilterChain chain):这个方法会在用户每次访问“目标资源(pattern>index.jsp)”时执行,如果需要“放行”,那么需要调用FilterChain的doFilter(ServletRequest,ServletResponse)方法,如果不调用FilterChain的doFilter()方法,那么目标资源将无法执行;
    • destroy():服务器会在创建Filter对象之后,把Filter放到缓存中一直使用,通常不会销毁它。一般会在服务器关闭时销毁Filter对象,在销毁Filter对象之前,服务器会调用Filter对象的destory()方法。
  • FilterConfig
    • ServletContext getServletContext():获取ServletContext的方法;
    • String getFilterName():获取Filter的配置名称;与元素对应;
    • String getInitParameter(String name):获取Filter的初始化配置,与元素对应;
    • Enumeration getInitParameterNames():获取所有初始化参数的名称。这里写图片描述
  • 多过滤器
    FilterChain#doFilter()方法:
    执行目标资源,或是执行下一个过滤器!如果没有下一个过滤器那么执行的是目标资源,如果有,那么就执行下一个过滤器!
  • 过滤器的四种拦截方式
  <dispatcher>REQUEST</dispatcher>默认的!  <dispatcher>FORWARD</dispatcher>  <dispatcher>INCLUDE</dispatcher>  <dispatcher>ERROR</dispatcher>

<filter-mapping>中进行配置!

  • REQUEST:直接访问目标资源时执行过滤器。包括:在地址栏中直接访问、表单提交、超链接、重定向,只要在地址栏中可以看到目标资源的路径,就是REQUEST;
  • FORWARD:转发访问执行过滤器。包括RequestDispatcher#forward()方法、标签都是转发访问;
  • INCLUDE:包含访问执行过滤器。包括RequestDispatcher#include()方法、标签都是包含访问;
  • ERROR:当目标资源在web.xml中配置为中时,并且真的出现了异常,转发到目标资源时,会执行过滤器。
  • 多个过滤器的执行顺序
    <filter-mapping>的配置顺序决定了过滤器的执行顺序!

  • 过滤器的应用场景

    • 执行目标资源之前做预处理工作,例如设置编码,这种试通常都会放行,只是在目标资源执行之前做一些准备工作;[几乎是的Sevlet中都需要写request.setCharacterEndoing() 可以把它入到一个Filter中]
    • 通过条件判断是否放行,例如校验当前用户是否已经登录,或者用户IP是否已经被禁用;
    • 在目标资源执行后,做一些后续的特殊处理工作,例如把目标资源输出的数据进行处理[回程拦截!];
0 0