Java Web Filter

Source: Internet
Author: User
Tags tomcat server

Java Filter (IMOOC learning)
definition : A filter is a server-side component that intercepts the client's request and response information and filters the information.

Working principle

1. Load the Web container when it starts up in the filter
2. The filter exists between the user request and the Web resource
3. The "Send and receive" of User request and Web resource response are filtered by filter according to "Filtering rules"

Life cycle

Instantiation--"initialize--" filter--"destroy
web.xml Init () DoFilter () Destroy ()


Web. XML configuration

First Filter Instance

1. Configure Web. xml

<filter>        <filter-name>myfilter</filter-name>        <filter-class>firstfilter</ filter-class>    </filter>    <filter-mapping>        <filter-name>myfilter</ filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

2. Write a class base filter

public class Firstfilter implements Filter{public void Destroy () {System.out.println ("destroy ..."); public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {System.out.println ("Start filter ..."); Chain.dofilter (request, response); System.out.println ("End Filter");} public void init (Filterconfig filterconfig) throws Servletexception {System.out.println ("init ....");//tomcat server runs when loading , can read Web. XML}}

When Tomcat boots, Output init, enter index.jsp in the browser, output start Filter,end filter, turn Tomcat off and execute destroy

  

  

Java Web Filter

Related Article

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.