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 manage shared database connections with Servlet listeners

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Servlet listener to manage shared database connections". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to manage shared database connections with Servlet listeners.

Overview of Servlet listener

◆ Listener is a listener for Servlet

◆ can listen to client requests, server operations, and so on.

Through the listener, ◆ can automatically trigger some operations, such as monitoring the number of online users, and when adding a HttpSession, add 1 to the number of online users.

◆ listeners need to implement the corresponding interface

After the ◆ is written, configure it in the web.xml file, and it will work.

◆ can increase the tracking of web application lifecycle events without modifying the existing system

Common Servlet listener listening interface

◆ ServletContextAttributeListener

Listen for operations on ServletContext properties, such as add / delete / modify

◆ ServletContextListener

Listen to the ServletContext and fire the contextInitialized (ServletContextEvent sce) method when the ServletContext is created and the contextDestroyed (ServletContextEvent sce) method when the ServletContext is destroyed.

◆ HttpSessionListener

Monitor the operation of HttpSession. The session Created (SessionEvent se) method is fired when a Session is created; when a Session is destroyed

The sessionDestroyed (HttpSessionEvent se) method is fired.

◆ HttpSessionAttributeListener

Listen for the operation of properties in HttpSession. The attributeAdded (HttpSessionBindingEvent se) method is fired when an attribute is added to Session, the attributeRemoved (HttpSessionBindingEvent se) method is fired when an attribute is deleted in Session, and the attributeReplaced (HttpSessionBindingEvent se) method is fired when the Session property is reset.

Examples of use:

Shared database connections are managed by Servlet listeners

A practical application of lifecycle events is that shared database connections are managed by context listeners. Define listeners in web.xml as follows:

< listener > < listener-class > XXX.MyConnectionManager < / listener-class > < / listener >

Server creates an instance of the listener, accepts events, and automatically determines the type that implements the listener interface. Keep in mind that because the listener is configured in the deployment descriptor web.xml, you can add a new listener without changing any code.

Public class MyConnectionManager implements ServletContextListener {public void contextInitialized (ServletContextEvent e) {Connection con = / / create connection e.getServletContext (). SetAttribute ("con", con);} public void contextDestroyed (ServletContextEvent e) {Connection con = (Connection) e.getServletContext (). GetAttribute ("con"); try {con.close () } catch (SQLException ignored) {} / / close connection}}

The Servlet listener ensures that each new servlet context generated will have an available database connection, and that all connection pairs will be closed when the context is closed.

Linstener that calculates the number of online users

(1) Package xxx

Public class OnlineCounter {private static long online = 0; public static long getOnline () {return online;} public static void raise () {online++;} public static void reduce () {online--;}} import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class OnlineCounterListener implements HttpSessionListener {public void sessionCreated (HttpSessionEvent hse) {OnlineCounter.raise () } public void sessionDestroyed (HttpSessionEvent hse) {OnlineCounter.reduce ();}}

In JSP where you need to show the number of people online, you can use the current number of people online:

<% @ page import= "xxx.OnlineCounter"% > <% = OnlineCounter.getOnline ()% >

Exit the session (you can provide the user with a logout button):

< form action= "exit.jsp" method=post > < input type=submit value= "exit" > < / form >

Exit.jsp: <% session.invalidate ();% >

Add the following to web.xml:

< listener > < listener-class > servletlistener111111.SecondListener < / listener-class > < / listener > at this point, I believe you have a better understanding of "how to manage shared database connections with Servlet listeners". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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