[Java Web development] Filte (filter), Listener (listener)

Filter Description:

Filter (filter) is Servlet technology is the most practical technology, Web developers, all Web resources Web server managed by Filter technology: for example Jsp, Servlet, still picture files or static html files to intercept, in order to achieve some special function. For example, implement URL-level access control, sensitive words filtered, compressed response information and other advanced features.

Servlet API provides a Filter interface, web application development, if written in the Java class that implements this interface, put the java class called filter Filter. Filter through technology, developers can implement user before accessing a target resource access requests and responses to intercept, as follows:

  

  • Filter is how to achieve interception

  Filter interface has a doFilter method, when we write good Filter, and configure after which web resources to intercept, WEB server each time before the service method is called web resources, it will first call doFilter filter method, therefore, in the write code within the method can achieve the following objectives:

  1. Before calling the target resources, so that a piece of code execution.

  2. Whether to call the target resource (ie whether to allow users to access web resources).

  3. After a call to target resources to execute a piece of code.

  web server when you call doFilter method, passed a filterChain objects come in, filterChain object is the filter interface of the most important objects, it also provides a doFilter method, developers can decide whether to call this method, this method is invoked on demand, the web server will call the web service resource methods that will be accessible web resources, web resources are not otherwise accessible.

Listeners Description:

Listener (Listener) for monitoring web applications, certain objects, create information, destruction, increase the occurrence of action to modify, delete, etc., and then make the appropriate response processing. When the range of the state of the object changes, the server automatically listener object method invocation. Commonly used in the statistics the number of online users and online, initialization information, visit the Web site statistics and so on when the system is loaded.

classification:

    Press Monitor object division, can be divided into

  • ServletContext object listener

  • HttpSession object listener

  • ServletRequest object listener

The event is divided according to listening

  • Object creation and destruction of their listeners

  • Property creation and the elimination of listener objects

  • The state of an object changes in the session listener

Want to learn more details: [Java Web development] filters and listeners

Ali cloud developer community fully upgraded, one-stop experience, with more cool :( Ali cloud developer community home page )

Guess you like

Origin blog.51cto.com/14377691/2414961