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; @WebServlet("/Counter0") public class Counter0 extends HttpServlet { int i = 0; // フィールドとして宣言する @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(""); out.printf("あなたは %d番目の来訪者です。", i++); out.println(""); out.close(); // closeを忘れない } }