Java-servlet Filters and Monitoring

Source: Internet
Author: User

1 Filter

  1. What is a filter?
    a special component defined in the servlet specification that intercepts the invocation of a container .
    Note: After the container receives the request , if there is a filter , the filter is called First and then the servlet is called.
  2. How do I write a filter ?
    1. Write a Java class to implement the Filter interface ;
    2. The interception method is implemented in the interface method ;
    3. Configuration filter (web. xml);
  3. Configuring Initialization Parameters
    1. Configure initialization Parameters . (Init-param)
    2. The initialized value is read by the getinitparamenter method provided by Filterconfig .
  4. Priority level :
    When there are multiple filters that meet the interception requirements , the containers are executed according to the order of the <filter-mapping> configuration.

2 Listeners

    1. What is a listener ?
      a special attribute defined in the servlet specification that listens to events generated by a container .
      Note : There are two major types of events

I) Declare a period-related event :
Refers to the event that occurs when a container creates or destroys a request,session,servlet Context object .

II) binding Data -related events :
Refers to when the setattribute,removeattrbute of the Request,session,servlet context is called The resulting event .

  1. How to write listener ?
    1. Write a java class implementation Listener interface
      Note: According to listener Event type select corresponding interface , For example : to listen session Create or destroy corresponds to events , need to implement httpsessionlistener interface
    2. in interface method , implementing Listening processing logic ;
    3. Configure listener (Web. xml).
  2. Serlvet context
    1. What is a servlet context ?
      once the container is started, a unique object that conforms to the servletcontext interface is created for each web App , which is the servlet context .
    2. Characteristics:

I) Uniqueness: a web application corresponds to a servlet context .

II) Persistence: as long as the server does not shut down and the app is not uninstalled, the context will always be there .

    1. How do I get the servlet context ?
      Genericservlet,servletconfig,filterconfig,httpsession provides a method (Getservletcontext)
    2. the role of the context

I) binding data
Setattribute,getattribute,removeattribute
Note: Compare:request<session< contexts from the length of time of survival
in the If the conditions of use are satisfied , the declaration period is shorter than the first use .

II) Read The global initialization parameters .

Java-servlet Filters and Monitoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.