Web. Xml detailed (Filter,context,listener)

Source: Internet
Author: User

Web. XML explained in detail!! Link

Web. XML load Process (step)
First, briefly, theloading process of Web. Xml. 
When we go to start a Web project, the container includes (JBoss,Tomcat, etc.) first read the configuration in the project's Web. XML configuration file, and when this step is not faulted and completed, the project can be started normally. 
  • When you start a Web project, read the node in XML: <listener></listener> and <context-param></ Context-param>
  • Container to create a ServletContext Object
    Example: You might want to open the database before the project starts, so here you can set up the database connection in <context-param> (driver,URL,user,password)  
  • The container then reads <filter></filter>, instantiating the filter according to the specified classpath.
  • These are the work that has already been done when the Web project is not fully up. If there is a servlet in the system , the servlet is instantiated the first time the request is initiated, and is generally not destroyed by the container, and it can serve the requests of multiple users. So, theServlet's initialization is much later than the ones mentioned above.
In general,the load order of Web. XML is

<context-param>-> <listener> <filter> <servlet>. where the same element appears in Web. XML, it is loaded according to the order in which it appears in the configuration file.

Introduction to Filter
1.Filter Description:
 filter can be considered as a "enhanced version" of servle, primarily for user requests request preprocessing, can also be response for post-processing, is a typical processing chain. The complete process for using filter is: filter pre-processing user requests and then servlet processing and generate a response, finally filter and then response to the server  HttpServletResponse for post-processing. filter and servlet have the exact same life cycle, and filter can also be <init-param> to configure the initialization parameters, and get filter The initialization parameters are used  Filterconfig getinitparameter ().  

In other words, theservlet has request and response two objects, andfilter can preprocess the request before a request arrives at the servlet , or leave when a servlet handles response, thefilter is actually a servlet chain.

Filter is responsible for intercepting multiple requests or responses, and one request or response can be intercepted by multiple filter. Creating a filter takes only two steps:

    • Creating a Filter processing class
    • Config Filter in the Web. xml file

The filter must implement the Javax.servlet.Filter interface, which defines three methods in the interface:

    • void Init (filterconfig configfilter. filteconfig is used to access configuration information for filter.
    • void Destroy (): for Filter to complete the collection of some resources before destroying it.
    • void doFilter (servletrequest request,servletresponse response,filterchain chainrequest, and can also implement post-processing lang= >response "en-US" to the server response chain.dofilter (request,response) before executing the method, which is a request to the user request is preprocessed, after the method is executed, the server responds response for post-processing.
2. Filter Configuration:
Filter is considered to be the "enhanced version" of the servlet, so the filter configuration is very similar to the configuration of the servlet and requires two parts: Configuring the Filter name and the filter interceptor URL pattern. The difference is that the servlet typically configures only one URL, while filter can configure multiple requested URLs at the same time . There are two ways of configuring the filter:          
    • The filter class is configured with annotation.
    • Configured in the Web. xml file through a configuration file.
We are using the Web. XML configuration, which focuses on some of the elements contained in <filter>. 
<filter> for specifying filters in the Web container, which can include <filter-name>,<filter-class>,< Init-param>,<icon>,<display-name>,<description>. 
    • <filter-name> is used to define the name of the filter, which must be unique throughout the program.
    • The <filter-class> element specifies the fully qualified name of the filter class, the implementation class of filter.
    • <init-param> Configure parameters for the filter, with the same element descriptors as <context-param> <param-name> and < Param-value>.
    • The <filter-mapping> element is used to declare a filter map in a Web application, and the filter is mapped to a servlet or a URL pattern. The <filter> and <filter-mapping> of this filter must have the same <filter-name>, specifying the Url. Filtering is performed in the order in which the <filter-mapping> of the deployment descriptor occurs.

Web. XML (EXT) (Filter,context,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.