⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initializelibrary.java

📁 使用Servlet和JSP进行Web组件开发
💻 JAVA
字号:
/* * InitializeLibrary.java * *  */package com.dvd.web;import com.dvd.model.DVDItem;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletContext;import javax.servlet.ServletContextListener;import javax.servlet.ServletContextEvent;/** * * @author   * @version * * Web application lifecycle listener. */public class InitializeLibrary implements ServletContextListener {    /**     * ### Method from ServletContextListener ###     *      * Called when a Web application is first ready to process requests     * (i.e. on Web server startup and when a context is added or reloaded).     *      * For example, here might be database connections established     * and added to the servlet context attributes.     */    public void contextInitialized(ServletContextEvent evt) {        List dvdList = new ArrayList();                // Get filename paramater from the web.xml file        ServletContext context = evt.getServletContext();        String libraryFile = context.getInitParameter("library-file");                InputStream is = null;        BufferedReader reader = null;        try {            is = context.getResourceAsStream(libraryFile);            reader = new BufferedReader(new InputStreamReader(is));            String line;                        // Read every dvd entry in the file            while ( (line = reader.readLine()) != null ) {                String[] elements = line.split("\\|");                                // Extract the data fields for the record                String title = elements[0];                String year = elements[1];                String genre = elements[2];                                // Add the new League item to the set                DVDItem item = new DVDItem(title, year, genre);                dvdList.add(item);            }                        context.setAttribute("dvdList", dvdList);                        context.log("The DVD Library has been loaded from: " + libraryFile );                    } catch (Exception e) {            context.log("Exception occured while processing the DVD library file.", e);                    } finally {            if ( is != null ) {                try { is.close(); } catch (Exception e) {}            }            if ( reader != null ) {                try { reader.close(); } catch (Exception e) {}            }        }    }    /**     * ### Method from ServletContextListener ###     *      * Called when a Web application is about to be shut down     * (i.e. on Web server shutdown or when a context is removed or reloaded).     * Request handling will be stopped before this method is called.     *      * For example, the database connections can be closed here.     */    public void contextDestroyed(ServletContextEvent evt) {        // TODO add your code here e.g.:        /*                Connection con = (Connection) e.getServletContext().getAttribute("con");                try { con.close(); } catch (SQLException ignored) { } // close connection        */    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -