Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package listener;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.annotation.WebListener;
- /**
- *
- * @author menf
- */
- @WebListener
- public class AppContextListener implements ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent servletContextEvent) {
- ServletContext ctx = servletContextEvent.getServletContext();
- String url = ctx.getInitParameter("DBURL");
- String user = ctx.getInitParameter("DBLOGIN");
- String pass = ctx.getInitParameter("DBPASS");
- DBConnection db;
- try {
- db = new DBConnection(url, user, pass);
- ctx.setAttribute("db", db);
- System.out.println("Database connection initialized.");
- } catch (SQLException ex) {
- Logger.getLogger(AppContextListener.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- @Override
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
- ServletContext ctx = servletContextEvent.getServletContext();
- DBConnection db = (DBConnection) ctx.getAttribute("db");
- try {
- db.closeConnection();
- System.out.println("Database connection closed for Application.");
- } catch (SQLException ex) {
- Logger.getLogger(AppContextListener.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Add Comment
Please, Sign In to add comment