読者です 読者をやめる 読者になる 読者になる

Servlet

Java

サーブレットをもう少し調べる機会があったので調べた。
セッションとかjspにデータを渡すところとか。

package bean;

public class Test {
    public String name;
}
package servlet;

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

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

/**
 * Servlet implementation class Servlet1
 */
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // response.getWriter().append("Served at:
        // ").append(request.getContextPath());
        try {
            execute(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private void execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String param1 = request.getParameter("param1");
        String id = null;
        HttpSession session = request.getSession(true);
        if (session.isNew()) {
            if (null != param1) {
                session.setAttribute("id", param1);
                session.setAttribute("count", 0);
                id = param1;
            }
        } else {
            if (null != param1) {
                id = (String) session.getAttribute("id");
                int count = (int) session.getAttribute("count");
                count++;
                if (count > 3) {
                    session.setAttribute("id", param1);
                    session.setAttribute("count", 0);
                } else {
                    session.setAttribute("count", count);
                }
            }
        }
        // sessionの終了
        // session.invalidate();
        // 期限の設定は、「WEB-INF/web.xml」ファイルで行います。
        // <session-config>
        // <session-timeout>30</session-timeout>
        // </session-config>
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.println("Hello!");
        if (null != param1) {
            writer.println("param1 is " + param1);
        }
        if (null != id) {
            writer.println(id);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Test;

/**
 * Servlet implementation class Servlet2
 */
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // response.getWriter().append("Served at:
        // ").append(request.getContextPath());
        Test t = new Test();
        t.name = "日本語太郎";
        try {
            request.setAttribute("test", t);
            request.getRequestDispatcher("/WEB-INF/jsp/Servlet2.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}