目次へ

解答例 - 実習課題1 - 7.リスナー

(実習課題1)

以下のWebアプリケーションを作成しなさい。

  • DBに保存されているリンク集(タイトル・URL)を表示する。
  • リンク集はアプリケーションの起動時に、DBから読み込み、ServletContextに登録すること。

解答例

/*
 * InitializationListener.java TECHSCORE Java Servlet7章 実習課題1
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.servlet.chapter7.exercise1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class InitializationListener implements ServletContextListener {

    public static final String URL_KEY = "urlKey";

    private Connection getConnection(ServletContext context)
            throws SQLException {
        String url = context.getInitParameter("url");
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        String driverClassName = context.getInitParameter("driverClassName");
        Connection conn = null;
        if (url != null && username != null && password != null
                && driverClassName != null) {
            try {
                Class.forName(driverClassName);
                conn = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                context.log(e.getMessage());
            }
        }
        return conn;
    }

    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        Connection conn = null;
        Map map = new HashMap();
        try {
            conn = this.getConnection(context);
            String sql = "SELECT name, url FROM link";
            Statement statement = conn.createStatement();
            ResultSet result = statement.executeQuery(sql);

            while (result.next()) {
                map.put(result.getString(1), result.getString(2));
            }
            context.setAttribute(InitializationListener.URL_KEY, map);
            result.close();
            statement.close();
        } catch (SQLException exception) {
            context.log("", exception);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException exception) {
            }
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        context.removeAttribute(InitializationListener.URL_KEY);
    }
}
/*
 * ListenerServlet.java TECHSCORE Java Servlet7章 実習課題1
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */
package com.techscore.servlet.chapter7.exercise1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListenerServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        StringBuffer buffer = new StringBuffer();
        buffer.append("<html><head><title>Servlet 7章実習課題 1");
        buffer.append("</title></head><body>");

        Map map = (Map)context.getAttribute(InitializationListener.URL_KEY);
        Iterator iterator = map.keySet().iterator();
        while(iterator.hasNext()){
            String name = (String)iterator.next();
            String url = (String)map.get(name);
            if (url.startsWith("http://")) {
                buffer.append("<a href=\"").append(url).append("\">");
                buffer.append(name).append("</a><br>");
            }
        }
        
        buffer.append("</body></html>");
        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter writer = response.getWriter();
        writer.println(buffer.toString());
    }
}

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp