File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes More Than One Object To A Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "More Than One Object To A Servlet" Watch "More Than One Object To A Servlet" New topic
Author

More Than One Object To A Servlet

abinash das
Ranch Hand

Joined: Apr 26, 2008
Posts: 52
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

What do you mean by "get an object to a servlet"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
abinash das
Ranch Hand

Joined: Apr 26, 2008
Posts: 52
No i mean hoe can i create more than one object to a Servlet ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Okay, then, what do you mean by "create an object to a servlet"?
abinash das
Ranch Hand

Joined: Apr 26, 2008
Posts: 52
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: 42951
    
  72
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: 12836
    
    5
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

Joined: Jul 10, 2008
Posts: 6
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.


Santosh Kolekar<br />SCJP,SCWCD,Preparing..SCBCD
 
 
subject: More Than One Object To A Servlet