Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More Than One Object To A Servlet

 
abinash das
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "get an object to a servlet"?
 
abinash das
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i mean hoe can i create more than one object to a Servlet ?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then, what do you mean by "create an object to a servlet"?
 
abinash das
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Santosh Kolekar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic