servlet是单态吗?

lovingfanglu 2009-01-05 01:25:10
Servlet是单态吗?
单态:内存中一个对象,那如果Test类下有一个静态的get()方法。
直接Test.get();这算单态吗?
还是必须在Test类个有一个公共的静态方法返回Test对象,构造方法定义为私有的才算单态...
...全文
752 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
中年風雨 2009-04-18
  • 打赏
  • 举报
回复
我也来凑凑热闹:
servlet不是单例的,不过一般情况下,servlet只有一个实例。
多数servlet是在被访问时由Container创建一个实例并初始化。此时servlet是单例模式。

如果servlet在Container启动时创建,则有可能会有多个实例。如下:
webl.xml中:

<servlet>
<servlet-name>ServletName1</servlet-name>
<servlet-class>test.ServetClass</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>ServletName2</servlet-name>
<servlet-class>test.ServetClass</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>ServletName1</servlet-name>
<url-pattern>/ServletName1</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>ServletName2</servlet-name>
<url-pattern>/ServletName2</url-pattern>
</servlet-mapping>


此时,servlet就有两个实例。
不过一般情况下,只配置一个servlet-mapping,也就只一个实例,也就相当于单例了。
rongyunsheng 2009-01-07
  • 打赏
  • 举报
回复
显然不是单例模式的,一般提到的都是单线程模式
我想单例模式跟单线程模式的区别,大家都晓得

特别要注意 Servlet及JSP中的多线程同步问题及servlet单线程模式
our651 2009-01-07
  • 打赏
  • 举报
回复
晕了我!
servlet有:
1、单线程模式
2、多线程模式(默认的servlet)

以上没有疑问吧,那么对应的:
1、单线程模式,一个web.xml注册的servlet名称会对每个request,新增一个servle实例
因为单线程,所以线程安全。
2、多线程模式,一个web.xml注册的servlet名称只有相应的一个实例存在,每个request
都是一个这实例在响应(多线程嘛),这时候线程不安全,要注意同步问题。

以上应该也没有疑问吧!
那么什么是单例模式呢?在概念上不就是和以上第二种情况一样吗?
你又要说了,我它的构造函数没有private啊。

参考JAVA与模式中对单例的解说,有一种不完全形态的单例,构造函数可以是public的。






HuanxueOrSeaty 2009-01-06
  • 打赏
  • 举报
回复
关注
hui_89418 2009-01-06
  • 打赏
  • 举报
回复
期待权威答案,谢谢
harryzyp 2009-01-06
  • 打赏
  • 举报
回复
servlet是单例的?应该不是吧...

怎么老大不解释下呢,大家都等着.
victorxiang 2009-01-06
  • 打赏
  • 举报
回复
仁者见仁智者见智
our651 2009-01-06
  • 打赏
  • 举报
回复
为什么都说吧是单例呢?
单例不就是只有一个实例吗?
ZiSheng 2009-01-06
  • 打赏
  • 举报
回复
单实例
dirtykiss 2009-01-06
  • 打赏
  • 举报
回复
servlet是线程安全的吗?困惑
our651 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 java2000_net 的回复:]
默认情况下servlet是singleton的 ?
谁告诉你的?
[/Quote]

Java Servlet Programming, 2nd Edition

3.4 Single-Thread Model
Although the normal situation is to have one servlet instance per registered servlet name, it is possible for a servlet to elect instead to have a pool of instances created for each of its names, all sharing the duty of handling requests. Such servlets indicate this desire by implementing the javax.servlet.SingleThreadModel interface. This is an empty, "tag" interface that defines no methods or variables and serves only to flag the servlet as wanting the alternate lifecycle.

A server that loads a SingleThreadModel servlet must guarantee, according to the Servlet API documentation, "that no two threads will execute concurrently in the servlet's service method." To accomplish this, each thread uses a free servlet instance from the pool, as shown in Figure 3-3. Thus, any servlet implementing SingleThreadModel can be considered thread safe and isn't required to synchronize access to its instance variables. Some servers allow the number of instances per pool to be configured, others don't. Some servers use pools with just one instance, causing behavior identical to a synchronized service( ) method.

Figure 3-3. The single-thread model

A SingleThreadModel lifecycle is pointless for a counter or other servlet application that requires central state maintenance. The lifecycle can be of some use, however, in avoiding synchronization while still performing efficient request handling.

For example, a servlet that connects to a database sometimes needs to perform several database commands atomically as part of a single transaction. Each database transaction requires a dedicated database connection object, so the servlet somehow needs to ensure no two threads try to access the same connection at the same time. This could be done using synchronization, letting the servlet manage just one request at a time. By instead implementing SingleThreadModel and having one "connection" instance variable per servlet, a servlet can easily handle concurrent requests because each instance has its own connection. The skeleton code is shown in Example 3-6.

pailman 2009-01-05
  • 打赏
  • 举报
回复
mark
梅子 2009-01-05
  • 打赏
  • 举报
回复
来学习~~
foxsh 2009-01-05
  • 打赏
  • 举报
回复
貌似问题多多哦!
记得Servlet肯定不可能是单态的。
applezhang1314 2009-01-05
  • 打赏
  • 举报
回复
Java Singleton 模式(单身模式,单态模式)是一种创建型设计模式。用来保证在运行的应用程序中,一个Class只是实例化一次,也就是只有一个相应的对象存在。
yjxqgd 2009-01-05
  • 打赏
  • 举报
回复
路过学习
yuanyangaas 2009-01-05
  • 打赏
  • 举报
回复
很想了解,说说看拉
fulianglove 2009-01-05
  • 打赏
  • 举报
回复
这个要看实现,默认的肯定不是。。。。
比如struts的ActionServlet就是singleton,只有一个。
zdyguilong 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hero272285642 的回复:]
引用 2 楼 java2000_net 的回复:
默认情况下servlet是singleton的 ?
谁告诉你的?


哈哈,老大有见解说一下嘛
[/Quote]

对啊,解释一下
MavenTalk 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 java2000_net 的回复:]
默认情况下servlet是singleton的 ?
谁告诉你的?
[/Quote]

哈哈,老大有见解说一下嘛
加载更多回复(7)

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧