In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "which java listeners". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn which java listeners are there.
1. Web listener 1. What is a web listener?
Web listeners are special classes in Servlet that help developers listen to specific events in web, such as the creation and destruction of ServletContext,HttpSession,ServletRequest, the creation, destruction and modification of variables, etc. You can add processing before and after some actions to achieve monitoring.
two。 Common uses of listeners
Web listeners are usually used to do the following:
Count the number of people online, using HttpSessionLisener
Load initialization information: using ServletContextListener
Statistics of website visits
Implement access monitoring
3. Next, take a look at the creation and execution of a listener.
First of all, you need to create a listener and implement a certain interface. For example, if I want to monitor the number of people online, you can create the following listener:
Public class MyListener implements HttpSessionListener {private int userNumber = 0; public void sessionCreated (HttpSessionEvent arg0) {userNumber++; arg0.getSession (). SetAttribute ("userNumber", userNumber);} public void sessionDestroyed (HttpSessionEvent arg0) {userNumber--; arg0.getSession () .setAttribute ("userNumber", userNumber);}}
Then configure the listener in web.xml and add to web-app:
Com.test.MyListener
Add the number of visitors to JSP:
Number of people online:
When I access it using my browser, the execution results are as follows:
Because opening another browser to access is equivalent to another session, the number of people online will increase.
For version 3. 0 of Servlet, annotated configuration is also supported.
So let's take a look at what listeners and methods are available.
Second, the classification of listeners 1. Divided according to the object of the monitor:
It can be divided into three categories according to the different listening objects:
ServletContext monitoring: corresponding to monitor the creation and destruction of application built-in objects.
The contextInitialized method is executed when the web container is opened, and the contextDestroyed method is executed when the container is closed or restarted.
Implementation method: directly implement the ServletContextListener interface:
Public class MyServletContextListener implements ServletContextListener {public void contextDestroyed (ServletContextEvent sce) {} public void contextInitialized (ServletContextEvent sce) {}}
HttpSession monitoring: corresponding to monitor the creation and destruction of session built-in objects.
When you open a new page, open a session session and execute the sessionCreated method; when the page closes, the session expires, or the container closes and destroys, the sessionDestroyed method executes.
Implementation method: directly implement the HttpSessionListener interface:
Public class MyHttpSessionListener implements HttpSessionListener {public void sessionCreated (HttpSessionEvent arg0) {} public void sessionDestroyed (HttpSessionEvent arg0) {}}
ServletRequest monitoring: corresponding to monitor the creation and destruction of request built-in objects.
When a page is visited, a request request is made and the requestInitialized method is executed; when the page is closed, the requestDestroyed method is executed.
Implementation method, directly implement ServletRequestListener interface:
Public class MyServletRequestListener implements ServletRequestListener {public void requestDestroyed (ServletRequestEvent arg0) {} public void requestInitialized (ServletRequestEvent arg0) {}} 2. Divided by monitoring events:
2.1 creation and destruction of the listening event itself: same as above by object.
2.2 additions, deletions and modifications of monitoring attributes:
The addition, deletion and modification of listening attributes are also divided into three categories, for ServletContext, HttpSession, and ServletRequest objects, respectively:
ServletContext to implement the ServletContextAttributeListener interface:
You can get the name of the property by calling the getName method of ServletContextAttribtueEvent.
Public class MyServletContextAttrListener implements ServletContextAttributeListener {public void attributeAdded (ServletContextAttributeEvent hsbe) {System.out.println ("In servletContext added: name =" >
HttpSession to implement the HttpSessionAttributeListener interface:
Public class MyHttpSessionAttrListener implements HttpSessionAttributeListener {public void attributeAdded (HttpSessionBindingEvent hsbe) {System.out.println ("In httpsession added:name =" + hsbe.getName ());} public void attributeRemoved (HttpSessionBindingEvent hsbe) {System.out.println ("In httpsession removed:name =" + hsbe.getName ()) } public void attributeReplaced (HttpSessionBindingEvent hsbe) {System.out.println ("In httpsession replaced:name =" + hsbe.getName ());}}
ServletRequest to implement the ServletRequestAttributeListener interface:
Public class MyServletRequestAttrListener implements ServletRequestAttributeListener {public void attributeAdded (ServletRequestAttributeEvent hsbe) {System.out.println ("In servletrequest added: name =" + hsbe.getName ());} public void attributeRemoved (ServletRequestAttributeEvent hsbe) {System.out.println ("In servletrequest removed: name =" + hsbe.getName ()) } public void attributeReplaced (ServletRequestAttributeEvent hsbe) {System.out.println ("In servletrequest replaced: name =" + hsbe.getName ());}}
2.3 status of the listening object:
For some POJO classes, you can listen for events of Pojo class objects by implementing the HttpSessionBindingListener interface. For example:
Public class User implements HttpSessionBindingListener,Serializable {private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password } public void valueBound (HttpSessionBindingEvent hsbe) {System.out.println ("valueBound name:" + hsbe.getName ());} public void valueUnbound (HttpSessionBindingEvent hsbe) {System.out.println ("valueUnbound name:" + hsbe.getName ());}}
Passivation and activation of Session data:
Because a large amount of important information related to visiting the website is stored in session, too much session data will degrade the performance of the server and occupy too much memory. So similar to the persistence of database objects, the web container persists infrequently used session data to local files or data. These are done by the web container itself and do not need to be set by the user.
The process of serializing unused session data to a local file is passivation
When the contents of the session are accessed again, the local file is read and put into memory again, which is called activation.
Similarly, as long as you implement the HttpSeesionActivationListener interface, you can listen for passivation and activation events:
Public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable {private String username; private String password; public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password } public void valueBound (HttpSessionBindingEvent hsbe) {System.out.println ("valueBound name:" + hsbe.getName ());} public void valueUnbound (HttpSessionBindingEvent hsbe) {System.out.println ("valueUnbound name:" + hsbe.getName ());} public void sessionDidActivate (HttpSessionEvent hsbe) {System.out.println ("sessionDidActivate name:" + hsbe.getSource ()) } public void sessionWillPassivate (HttpSessionEvent hsbe) {System.out.println ("sessionWillPassivate name:" + hsbe.getSource ());}} III. Servlet version and Tomcat version
First of all, take a look at the match given on the official website of Tomcat:
If the version does not match, then tomcat cannot release the project. First, take a look at what happens when the version does not match!
I tried to create a web project and chose the Servlet3.0 version:
Solution:
1 when creating, publish directly to the Tomcat container. In this case, Servlet will only list the versions supported by Tomcat:
2 modify the Servlet version configuration information of the project. The file is: working directory\ SessionExample.settings\ org.eclipse.wst.common.project.facet.core.xml
IV. The difference between getAttribute and getParameter
This part is an extension of JSP, often fetching data in JSP or Servlet, so what's the difference between getAttribute and getParameter?
1. In terms of the source of the data obtained:
GetAttribtue gets the value in the web container, such as:
We set a value in Servlet through setAttribute, which exists in the container and can be obtained by the getAttribute method.
What getParameter gets is the value passed through http, such as this http request:
Http:localhost:8080/test/test.html?username=xingoo
There are other GET and POST methods, all of which can be obtained through getParameter.
two。 In terms of the type of data obtained:
GetAttribute returns an object, Object.
What getParameter returns is that the value passed by a form or parameter after http in the previous page is a string.
Thank you for your reading, the above is the content of "which java listeners", after the study of this article, I believe you have a deeper understanding of the question of which java listeners are available, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.