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

标签 java multithreading jsp servlets

我想创建一个线程安全的 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/

相关文章:

Android 应用程序没有响应后退按钮

java - run 方法内的同步块(synchronized block)

java - 需要一个在jsp中读取动态复选框ID的javascript函数

java - 如何在 jQuery 中访问 Struts 2 变量

java - Jquery 无法获取我的 jsp 页面中 session 的最新值

java - <Thinking in java> swich(..) {....} 中的一些代码如果默认意味着什么都不做,为什么要设置默认值?

java - 在spring boot中显示jsp页面时,它与jsp头文件一起显示

Java:如何从 HTML 标签中剥离文本内容?

C# - 在不同的线程中添加 UserControl 即使在调用之后也会导致异常

java - 如何测试 Spring RESTful MVC Controller 方法?