gpt4 book ai didi

java - 如何创建线程安全的 JSP 页面

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:03:32 26 4
gpt4 key购买 nike

我想创建一个线程安全的 JSP 页面。在 Servlet 中通过实现 SingleThreadModel 接口(interface)是可能的,但我不知道如何在 JSP 页面中实现。

最佳答案

理论上,JSP 页面可以通过 isThreadSafe 页面指令属性指示为线程安全的。将值设置为 false,将使容器同步访问页面级对象(而不是 session 和应用程序范围的对象或任何其他种类的对象)。显然,确保同步访问代码的线程不安全区域仍然是开发人员的责任。

此外,SingleThreadModel 接口(interface)在 Servlet 规范 2.4 版中也已弃用。 SingleThreadModel 接口(interface)也用于在 JSP 中实现假定的线程安全 - 生成的 servlet 类将为使用线程安全属性的 JSP 实现 SingleThreadModel。规范本身概述了不推荐使用该接口(interface)的原因:

SRV.2.2.1 Note About The Single Thread Model

The use of the SingleThreadModel interface guarantees that only one thread at a time will execute in a given servlet instance’s service method. It is important to note that this guarantee only applies to each servlet instance, since the container may choose to pool such objects. Objects that are accessible to more than one servlet instance at a time, such as instances of HttpSession, may be available at any particular time to multiple servlets, including those that implement SingleThreadModel.

It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. The SingleThreadModel Interface is deprecated in this version of the specification.

关于java - 如何创建线程安全的 JSP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3572712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com