In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are JavaWeb listeners and filters". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are JavaWeb listeners and filters".
1. Listener-> Context,Session
What is listener?
The listener is an interface that we implement and will be called at a specific time. The listener is used to listen to the three major domain objects (request,session,application) in web applications, create, destroy, add, modify, delete and other actions, and then respond accordingly. When the state of the scope object changes, the server automatically calls the method in the listener object. It is often used to count the number of online people and online users, initialize information when the system is loaded, and count the number of visits to the website.
ContextListener implements ServletContextListener for global snooping
ContextListener can record the number of times users visit the site: users can access the hashmap stored in the listener by visiting index.jsp
< String,Integer>And then make the judgment in index.jsp
The code idea of ContextListener is as follows:
Public class ContextListener1 implements ServletContextListener {@ Override public void contextInitialized (ServletContextEvent servletContextEvent) {System.out.println ("init"); / / create the map collection Map map = new HashMap (); / / get the global object ServletContext context = servletContextEvent.getServletContext (); context.setAttribute ("map", map); System.out.println (map.isEmpty ()); System.out.println (map) @ Override public void contextDestroyed (ServletContextEvent servletContextEvent) {System.out.println ("destory");}}
The code idea of index.jsp is as follows:
The ip address is: you are the first user to access. The current server has been accessed by a total of 2. 5 users. Three scopes of listeners
3. Property listener
Property listeners mainly listen for changes in property values, such as request.setAttribute () and so on.
Package listener;import javax.servlet.*;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent / * * @ author wjs * @ create 2022-02-27 15:09 * / public class AttrListener implements ServletContextAttributeListener, ServletRequestAttributeListener HttpSessionAttributeListener {@ Override public void attributeAdded (ServletContextAttributeEvent servletContextAttributeEvent) {/ / listeners that add values to the global scope} @ Override public void attributeRemoved (ServletContextAttributeEvent servletContextAttributeEvent) {/ / listeners that delete values to the global scope} @ Override public void attributeReplaced (ServletContextAttributeEvent servletContextAttributeEvent) {/ / listeners that modify values to global objects} @ Override public void attributeAdded ServletRequestAttributeEvent servletRequestAttributeEvent) {/ / listeners that add values to request field} @ Override public void attributeRemoved (ServletRequestAttributeEvent servletRequestAttributeEvent) {/ / listeners that delete values from request field} @ Override public void attributeReplaced (ServletRequestAttributeEvent servletRequestAttributeEvent) {/ / listeners that modify values in request field} @ Override public void attributeAdded (HttpSessionBindingEvent httpSessionBindingEvent) {/ / add values to session fields Listeners} @ Override public void attributeRemoved (HttpSessionBindingEvent httpSessionBindingEvent) {/ / listeners that delete values to session fields} @ Override public void attributeReplaced (HttpSessionBindingEvent httpSessionBindingEvent) {/ / listeners that modify values in session fields}} 4. Filter
4.1 use of filters 1. Write java class to implement Filter interface 2. Override doFilter () method 3. Set the intercepting path of the intercepted url4.2 filter
/ *: all requests under the root directory are intercepted
/ * .do: all requests with .do are intercepted
/ * .jsp
4.3 intercept sequence of filter
The order in which the filter is intercepted depends on the order in the configuration file web.xml
4.4 four ways to intercept filters
The above is all the content of the article "what are the listeners and filters of JavaWeb". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.