Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Filter,Listener and Servlet of Web

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Filter,Listener and Servlet of Web". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Filter,Listener and Servlet of Web" can help you solve the problem.

Filter: filter 1. Concept: * filters in life: water purifiers, air purifiers, bandits, * filters in web: when accessing the resources of the server, the filter can intercept requests and perform some special functions. * the function of the filter: * it is generally used to complete general operations. Such as: login authentication, unified coding processing, sensitive character filtering. 2. Getting started: 1. Step: 1. Define a class and implement interface Filter 2. 0. Copying method 3. Configure intercept path 1. Web.xml 2. Note 2. Code: @ WebFilter ("/ *") / / before accessing all resources, the filter public class FilterDemo1 implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println ("filterDemo1 is executed.") / / release filterChain.doFilter (servletRequest,servletResponse);} @ Override public void destroy () {}} 3. Filter details: 1. Web.xml configuration demo1 cn.itcast.web.filter.FilterDemo1 demo1 / * 2. Filter execution flow 1. Execute filter 2. Execute resources after release 3. Come back and execute the code under the filter release code 3. Filter life cycle method 1. Init: after the server starts, the Filter object is created, and then the init method is called. It's only done once. Used to load resources 2. DoFilter: each time a resource is intercepted, it is executed. Execute 3. Destroy multiple times: after the server is shut down, the Filter object is destroyed. If the server shuts down normally, the destroy method is executed. It's only done once. Used to release resources 4. Filter configuration details * Intercept path configuration: 1. Specific resource path: / index.jsp only when accessing index.jsp resources, the filter will be executed 2. Intercept directory: / user/* accesses all resources under / user, the filter will be executed 3. Suffix name interception: when jsp accesses all resources with the suffix jsp, the filter will be executed 4. Block all resources: / * when accessing all resources, the filter will be executed * intercept configuration: how resources are accessed * Note configuration: * set dispatcherTypes attribute 1. REQUEST: default. Browsers directly request resources 2. FORWARD: forward access resources 3. INCLUDE: contains access resources 4. ERROR: error redirects resources 5. ASYNC: asynchronous access to resources * web.xml configuration * set the label 5. Filter chain (configure multiple filters) * execution order: if there are two filters: filter 1 and filter 2 1. Filter 1 2. Filter 2 3. Resource execution 4. Filter 2 5. Filter 1 * filter sequence problem: 1. Note configuration: compare according to the string comparison rules of class names, and those with small values will be executed first * such as AFilter and BFilter,AFilter. 2. Web.xml configuration: who is defined above and who executes Listener: listener * concept: one of the three major components of web. * event listening mechanism * event: one thing * event source: where the event occurs * listener: an object * registered listener: bind event, event source and listener together. When an event occurs on the event source, execute the listener code * ServletContextListener: listen for the creation and destruction of the ServletContext object * method: * void contextDestroyed (ServletContextEvent sce): this method will be called before the ServletContext object is destroyed * void contextInitialized (ServletContextEvent sce): this method will be called after the ServletContext object is created * step: 1. Define a class and implement ServletContextListener interface 2. 0. Copying method 3. Configuration 1. Web.xml cn.itcast.web.listener.ContextLoaderListener * specifies initialization parameter 2. Note: * @ WebListenerservlet* concept: Mini Program * Servlet running on the server is an interface that defines the rules for Java classes to be accessed by browsers (identified by tomcat). * in the future, we will customize a class, implement the Servlet interface and override the method. * * Quick start: 1. Create JavaEE project 2. Define a class that implements the Servlet interface * public class ServletDemo1 implements Servlet3. Implement the abstract method in the interface 4. Configure Servlet in web.xml configuration: demo1 cn.itcast.web.servlet.ServletDemo1 demo1 / demo1 * implementation principle: 1. When the server receives the request from the client browser, it parses the request URL path and gets the resource path 2. 5 of the accessed Servlet. Find the web.xml file to see if there is a corresponding tag body content. 3. If so, the corresponding full class name 4. Tomcat loads the bytecode file into memory and creates its object 5. Call the life cycle method in its method * Servlet: 1. Created: execute the init method, only once * when will the Servlet be created? * by default, the Servlet is created when it is accessed for the first time * you can configure the time when the Servlet is created. * configure 1. When accessed for the first time, the value of create * is negative 2. When the server starts, create an init method with a value of 0 or a positive integer * Servlet, which is executed only once, indicating that a Servlet has only one object in memory, and Servlet is a single instance * thread safety problems may exist when multiple users access it at the same time. * solution: try not to define member variables in Servlet. Even if a member variable is defined, do not modify the value 2. Providing services: the service method is executed multiple times * the Service method is called once each time the Servlet is accessed. 3. Destroyed: the destroy method is executed only once * when Servlet is destroyed. When the server is shut down, the Servlet is destroyed * the destroy method is executed only when the server is shut down normally. * the destroy method is executed before the Servlet is terminated and is generally used to release resources * Servlet3.0:* benefits: * Annotation configuration is supported. You don't need web.xml anymore. * step: 1. To create a JavaEE project, you can choose Servlet version 3. 0 or above without creating web.xml 2. 0. Define a class and implement Servlet interface 3. 0. Copying method 4. Use the @ WebServlet annotation on the class to configure * @ WebServlet ("Resource path"). This is the end of the content on "how to use Filter,Listener and Servlet of Web". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report