与服务器实例混淆

发布于 2024-12-26 22:35:27 字数 1224 浏览 0 评论 0原文

我有下面的一段代码,它工作正常,没有问题,但我很困惑,如何制作多个实例。每次运行此代码时,我都会得到“有 1 个实例正在运行”

public class HolisticCounter extends HttpServlet {


    private static final long serialVersionUID = 1L;
static int classCount = 0;  // shared by all instances
  int count = 0;              // separate for each servlet
  static Hashtable instances = new Hashtable();  // also shared

  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    count++;
    out.println("Since loading, this servlet instance has been accessed " +
                count + " times.");

    // Keep track of the instance count by putting a reference to this
    // instance in a Hashtable. Duplicate entries are ignored. 
    // The size() method returns the number of unique instances stored.
    instances.put(this, this);
    out.println("There are currently " + 
                instances.size() + " instances.");

    classCount++;
    out.println("Across all instances, this servlet class has been " +
                "accessed " + classCount + " times.");
  }
}

I have the following piece of code its working fine,no issues but i am confused,how to make more than one instances of this.Every time i run this i get "there are 1 instances running"

public class HolisticCounter extends HttpServlet {


    private static final long serialVersionUID = 1L;
static int classCount = 0;  // shared by all instances
  int count = 0;              // separate for each servlet
  static Hashtable instances = new Hashtable();  // also shared

  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    count++;
    out.println("Since loading, this servlet instance has been accessed " +
                count + " times.");

    // Keep track of the instance count by putting a reference to this
    // instance in a Hashtable. Duplicate entries are ignored. 
    // The size() method returns the number of unique instances stored.
    instances.put(this, this);
    out.println("There are currently " + 
                instances.size() + " instances.");

    classCount++;
    out.println("Across all instances, this servlet class has been " +
                "accessed " + classCount + " times.");
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2025-01-02 22:35:27

在绝大多数情况下,每个 条目只有一个 servlet 实例。容器不再需要实例化,因此它为每个请求重用相同的实例。

服务器允许实例化多个,但通常没有理由这样做。

In the vast majority of cases, there is only ever one servlet instance per <servlet> entry. The container does not need to instantiate any more, and so it reuses the same instance for every request.

The server is permitted to instantiate several, but there's usually no reason for it to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文