Servlet中的静态变量

时间:2016-01-01 19:05:08

标签: java servlets web-applications

我对Servlets很新。我无法弄清楚以下代码的行为。

public class HelloServlet extends HttpServlet {

  static int counter = 0;

  public void doGet(HttpServletRequest req, HttpServletResponse res)
          throws IOException {
      if (counter == 0) {
          httpServletResponse.getWriter().print("Counter = " + counter);
          counter++;
      } else {
          httpServletResponse.getWriter().print("Counter = " + counter);
      }
   }
}

当我部署并运行它时,结果为" Counter = 1"在浏览器中。为什么跳过" Counter = 0"直接打印" Counter = 1"?

0 个答案:

没有答案