Is There Any Way By which we can get More than one object to a Servlet.Actually in one interview i faced this question. [ July 23, 2008: Message edited by: Bear Bibeault ]
Okay, then, what do you mean by "create an object to a servlet"?
abinash das
Ranch Hand
Joined: Apr 26, 2008
Posts: 52
posted
0
Normally the concept of Servlet is like, Only 1 Sevlet Object created per Servlet when 1st request hits the Servlet. But through load on startup we are getting on object to a Servlet at the time of starting of the Server, but if i want more than one Servlet Objects, is there any way by which i can achieve it.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
Are you asking about the number of object instances that gets created from a servlet class? If so, generally only a single instance is created. It makes no difference whether it's created at startup (through load-on-startup) or at the first request - it's still only a single instance.
There can be several instances of the same servlet in a single web app context only if they are configured under different names (and possibly with different parameters and different URLs) in the web.xml file. [ July 23, 2008: Message edited by: Ulf Dittmer ]
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
Assuming you mean an instance of a servlet class, yes. Every different web application can have its own instance of a given servlet addressed by its own URL and managed separately by the servlet container. Just read the servlet API and this sort of thing will become clear.
As per Servlet specification only one object & multiple threads(if not implemented SingleThreadModel) get created for that servlet in web application. but if you want to create multiple objects: 1. As Ulf said you can configure that servlet with diffrent names. 2. you can keep multiple copies of same servlet in diffrent package and congigure that in web.xml 3.Write that servlet in multiple web applications(web.xml) and syncronize them. and for dispatch each request bu writing your own dispatcher so that for each request get forwards to diffrent web applictions.
Santosh Kolekar<br />SCJP,SCWCD,Preparing..SCBCD
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.