Java Web Learning eighteen: Filter and Listener

A: Filter Filter

Filter the name implies, the filter web, at the time the server is accessed resources, the request will be intercepted Filter , perform specific functions. Usually perform common operations, such as: login authentication, Unicode, filtered sensitive character ...

1. How to configure a filter?

(1) IDEA support to configure intercept path with annotations, such as @WebFilter ( "/ *") // before accessing all resources, will implement the filter

(2) the web.xml configuration: Filter and Servlet as, web with web, xml can be configured in the same format as is generally Servlet

<filter>
    <filter-name>demo1</filter-name>
<!--        Filter所在位置-->
    <filter-class>cn.itcast.web.filter.FilterDome1</filter-class>
</filter>

<filter-mapping>
    <filter-name>demo1</filter-name>
<!--        拦截路径-->
    <url-pattern>/*</url-pattern>
</filter-mapping>

Process (doFilter in execution) 2.Filter performed

(1) perform filter

Resource filterChain.doFilter (servletRequest, servletResponse) after (2) performing release; release means is permitted to access the resource

(3) back below the execution code Code release

code show as below:

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    //对request对象请求消息进行增强
    System.out.println("FilterDome执行...");

    //放行
    filterChain.doFilter(servletRequest,servletResponse);

    //对response对象的响应消息进行增强
    System.out.println("FilterDome再次的执行");
}

Note: When continuous access to resources, each will perform in doFilter method Filter

3.Filter life cycle

Filter has a default init doFilter destroy three require replication method, so its life cycle

1.init

Is the server starts, it creates a Filter object, and then call the init method, init method performs only once for loading resources

2.doFilter

Each request (when accessing resources) when intercepted, will perform, and doFilter method will be executed multiple times (several times to intercept execution times)

3.destroy

After the server is shut down, Filter out objects are also destroyed, server normally closed, then performs destory method, is performed only once, to free up resources

4.Filter interception configuration in detail

* Intercept path configuration:

(1) specific resource path: /index.jsp only access resources when index.jsp, Filter will be executed

(2) block a directory (so resources under a directory): / user / Only access to resources under / user, Filter will be executed *

(3) extension of the interception: When * jsp jsp suffix access to all resources, Filter will be executed

(4) intercept all resources: time / * access to all resources, Filter will be executed

* Configure interception mode: the way resources are accessed

* IDEA comments configuration:

    *设置dispatcherTypes的属性

      (1)REQUEST:为dispatchTypes的默认值,浏览器直接请求资源。
      
      (2)FORWARD:转发来访问资源

      (3)INCLUDE:包含访问资源

      (4)ERROR:错误跳转资源

      (5)ASYNC:异步访问资源


For example @WebFilter (value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST), the browser requests index.jsp resources directly, the resources will be executed so that access to the filter

May also overlay @WebFilter (value = "/index.jsp", {DispatcherType.REQUEST, DispatcherType.FORWARD}) browser directly or forward access requests index.jsp resources, access to the resources so that the filter will be executed

*web.xml配置:

    * 设置<dispatcher></dispatcher>标签即可

The filter chain (a plurality of filters arranged)

(1) The order of execution: if there are two filters: Filter 1 and Filter 2

  1. Filter 1

  2. Filter 2

  3. Resources to implement

  4. Filter 2

  5. Filter 1

Note: Similar to the recursive call


2) order filters the problem:

1. Configuration Annotations: comparison string comparison rules in accordance with the class name, the first value is smaller performed

  • Such as: AFilter and BFilter, AFilter on the first executed.
  1. web.xml Configuration: who defines the top, the first to perform.

Two: Filter Use Cases

1. login authentication

  • demand:
  1. Access day17_case case of resources. Verify whether the login

  2. If you are logged in, the direct release.

  3. If not logged in, go to the login page, it says "You are not logged in, please login."

    1. Case sensitive words filtered 2_
  • demand:
  1. Day17_case case of data entry will be sensitive words filtered

  2. Sensitive words refer to "sensitive words .txt" (a self-built documentation sensitive words)

  3. If you are sensitive vocabulary, replaced ***

  • analysis:
  1. The object of the request enhanced. Enhancing acquisition parameters related methods

  2. Release. Delivery Agent objects

  • Enhanced Object features:

  • Design Patterns: some of the ways to solve common problems fixed

Example: Lenovo agents sell computers

  1. Decorative pattern

  2. Proxy mode

  • concept:
  1. The real object: The object being proxied

2. The proxy object:

3. Proxy mode: proxy proxy object real object, the purpose of enhancing the function of a real object

  • Method to realize:
  1. Static agent: there is a class file description proxy mode

  2. Dynamic Agent: Agent class is formed in memory

  • Implementation steps:
  1. Proxy object and real object implement the same interface

2. The proxy object = Proxy.newProxyInstance ();

3. Use a proxy object to call methods.

  1. Enhancement
  • Enhanced mode:
  1. Enhanced parameter list

  2. Enhance the return value type

  3. Enhancement body to perform logic

    to realize Case: sensitive words 在这里插入图片描述

    when enhanced getParameter method that is sensitive words will be displayed ***

Three: Listener Listener

1. Concept: One of the web of the three components (servlet, filter, listener)

(1) event listeners mechanism

Event: One thing

Local events (carried out): Event Source

Listener: An object created

Register listeners: event, event source, the listener bound, when an event occurs event source, the listener code execution

Creating and destroying listening ServletContext object: (2) ServletCpntextListener

method:

void contextDestroyed (ServletContextEvent sce): This method is called before the object is destroyed ServletContext

void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调用该方法

步骤:

1.定义一个类,实现ServletContextListener接口,并复写起方法

2,配置

(1)web.xml

      <listener-class>cn.itcast.web.listener.ContextLoaderListener</listener-class>

指定初始化参数

(2)IDEA注解

发布了47 篇原创文章 · 获赞 18 · 访问量 4883

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/96478527
Recommended