1 Filter
- 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.
- How do I write a filter ?
- Write a Java class to implement the Filter interface ;
- The interception method is implemented in the interface method ;
- Configuration filter (web. xml);
- Configuring Initialization Parameters
- Configure initialization Parameters . (Init-param)
- The initialized value is read by the getinitparamenter method provided by Filterconfig .
- 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
- 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 .
- How to write listener ?
- 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
- in interface method , implementing Listening processing logic ;
- Configure listener (Web. xml).
- Serlvet context
- 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 .
- 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 .
- How do I get the servlet context ?
Genericservlet,servletconfig,filterconfig,httpsession provides a method (Getservletcontext)
- 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