Java-web Filter Listener

Source: Internet
Author: User

servlet specification
Servlet/filter/listener/interceptor Difference and Contact
http://blog.csdn.net/sundenskyqq/article/details/8549932
http://hejiajunsh.iteye.com/blog/1776571

Application: The context object, created and initialized by the container, is scoped to the entire Web application, and only one application object exists throughout the Web application, and the Application object is destroyed when the Web server shuts down
Session: is a mechanism to save context information, it is for each session, it is placed on the server side, through the SessionID, after the browser and server end, will be destroyed session
Request object: The container creates a ServletRequest object to encapsulate the request data when the client sends the requests, and creates a Servletresponse object to encapsulate the corresponding data, which is destroyed when the encapsulation request is closed
The Web listener is the one that listens to the creation, destruction, and changes of the properties of these 3 objects.
Purpose: Statistics online number of people, statistics visits, system variable initialization, Spring

Use:
1. Create an interface that inherits the corresponding
2. Web. XML registration
A application, HttpSession, ServletRequest can have multiple listeners
ServletContext Global one, using the global parameters of Context-parm. Used when the container starts at the end.
HttpSession multiple, Httpsessionlistener can have more than one. When creating a destroy, run
Web. XML to set the session timeout properties
on the properties in the listening domain (attribute, put in the request field, parameter is the parameter), and delete and listen
HttpSession Binding is setattribute () passivation activation is because there are too many sessions, memory is not enough, serialized to a file. Loaded automatically when overloaded.
1. Domain Object
session/request/application
2. Event
Create/Destroy/attribute change/servlet state Change
3. Order
Listener-Filter->servlet
Ps:webx, struts are injected through the filter because this is flexible enough

    1. Determine whether the target resource needs to be accessed by controlling the invocation of the Chain.dofilter method.
      For example, user permissions can be verified and so on. Determine whether the user has access to certain resources, have permission to release, do not have permission to execute the Chain.dofilter method.
    2. By doing some processing before calling the Chain.dofilter method, you can achieve some purpose.
      For example, solve the problem of Chinese garbled and so on. The encoding of the request encoding and response can be performed before the Dofilter method. The request interface can even be encapsulated to handle the Chinese garbled problem of Get requests (overriding the corresponding Request.getparameter method).
    3. After calling the Chain.dofilter method, do some processing to achieve some purpose.
      For example, the entire Web site compression. Use Class A to encapsulate the response object before calling the Chain.dofilter method, overriding the Getoutputstream and overriding the Getwriter method. Within Class A, the output is cached into the Bytearrayoutputstream stream. Then after the Chain.dofilter method executes, gets the Bytearrayoutputstream stream cache data in Class A, compressed with the Gzipoutputstream stream.

The execution flow of the filter is in the order of filter-mapping, where Filterchain is the filter to be executed later
Init and destory are executed only once when the container is initialized/destroyed.
The execution flow of the filter:
The code before the Chain.dofilter () that executes the first filter () and the code before the second filter's Chain.dofilter ()-->......--> the Chain.dofilter () of the nth filter-- > Code in the Service () method of the requested Servlet--the code in the Doget () or Dopost () method of the requested servlet--and the nth filter after Chain.dofilter ()--> ...--> the code after the second filter's Chain.dofilter () and the first filter's Chain.dofilter ().

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-web Filter Listener

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.