昨日書いた記事を検証してみた。
以下3つの例を検証。
1.単純ケース
@WebServlet(name = "test01", urlPatterns = {"/test01"}) public class Test01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); try (PrintWriter writer = resp.getWriter();) { resp.setStatus(200); resp.setContentType("text/plain"); writer.write("name is " + name); writer.flush(); writer.close(); } } }
2.絶対ダメなケース
@WebServlet(name = "test02", urlPatterns = {"/test02"}) public class Test02 extends HttpServlet { private String name; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { name = req.getParameter("name"); try (PrintWriter writer = resp.getWriter();) { resp.setStatus(200); resp.setContentType("text/plain"); writer.write("name is " + name); writer.flush(); writer.close(); } } }
3.今回検証したかったケース
@WebServlet(name = "test03", urlPatterns = {"/test03"}) public class Test03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { new MyClass(req, resp).execute(); } class MyClass { private HttpServletRequest req; private HttpServletResponse resp; private String name; public MyClass(HttpServletRequest req, HttpServletResponse resp) { this.req = req; this.resp = resp; name = req.getParameter("name"); } public void execute() throws IOException { try (PrintWriter writer = resp.getWriter();) { resp.setStatus(200); resp.setContentType("text/plain"); writer.write("name is " + name); writer.flush(); writer.close(); } } } }
結論としては、3は大丈夫であった。
jmeterで同時接続100を10回やって、一度も不整合は起きず。