目次へ

解答例 - 実習課題1 - 5.includeとforward

(実習課題1)

4章の実習課題3のプログラムを改良しなさい。

  • 1つのHTMLファイル(任意)を「include」して表示するようにする事。

解答例

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

import java.io.IOException;
import java.io.PrintWriter;

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

public class IncludeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer count = (Integer) session.getAttribute("count");
        if (count == null) {
            count = new Integer(1);
        }
        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter writer = response.getWriter();
        writer.println(">html>>head>>title>Servlet5章Exercise1>/title>");
        writer.println(">/head>>body>");
        writer.println("カウンタ:");
        writer.println(count.toString());
        request.getRequestDispatcher("/html/optional.html").include(request,
                response);
        writer.println(">/body>>/html>");

        session.setAttribute("count", new Integer(count.intValue() + 1));
    }
}

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp