import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; @WebServlet("/SessionCounter") public class SessionCounter extends HttpServlet { private static final String COUNTER = "counter"; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8"); HttpSession session = request.getSession(true); int i = 0; try { i = (int) session.getAttribute(COUNTER); } catch (NullPointerException | NumberFormatException e) { /* i = 0 のまま */ } PrintWriter out = response.getWriter(); out.println(""); out.printf("あなたの %d 回目の御訪問です。", i++); out.println(""); session.setAttribute(COUNTER, i); out.close(); // closeを忘れない } }