如何创建Thread安全JSP页面 [英] How can I create Thread safe JSP page

查看:180
本文介绍了如何创建Thread安全JSP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Thread安全的JSP页面。通过实现SingleThreadModel接口可以在Servlet中实现,但我不知道如何在JSP页面中执行它。

I want to create a Thread safe JSP page. It is possible in Servlet by implementing SingleThreadModel interface but I don't know how to do it in JSP page.

推荐答案

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

Theoretically, JSP pages can be indicated as threadsafe via the isThreadSafe page directive attribute. Setting a value of false, will get the container to synchronize access to page level objects (and not to session and application scoped objects or objects of any other variety). Obviously, it is still the responsibility of the developer to ensure that synchronous access to thread unsafe regions of code.

Morevoer,在Servlet规范中也不推荐使用SingleThreadModel接口发布2.4。 SingleThreadModel接口用于在JSP中实现假定的线程安全性 - 生成的servlet类将为使用threadsafe属性的JSP实现SingleThreadModel。规范本身概述了不推荐使用接口的原因:

Morevoer, the SingleThreadModel interface has also been deprecated in the Servlet Specification release 2.4. The SingleThreadModel interface is used to implement the supposed thread safety in JSPs as well - generated servlet classes will implement the SingleThreadModel for JSPs that use the threadsafe attribute. The specification itself outlines why the interface is deprecated:


SRV.2.2.1关于单线程模型的注意事项

使用SingleThreadModel
接口保证一次只有一个
线程将在
给定的servlet实例的服务$中执行b $ b方法。重要的是要注意
此保证仅适用于每个
servlet实例,因为容器
可以选择池化这些对象。
一次可访问多于
的对象,一次是一个servlet实例,
,例如HttpSession实例,可以在任何特定时间以
多个servlet获得
,包括那些实现SingleThreadModel的

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.

建议开发人员
采取其他方式解决这些
问题而不是实施这个
接口,例如避免使用实例变量

同步访问这些资源的代码块

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.

这篇关于如何创建Thread安全JSP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆