This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Explicitly instance of Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Explicitly instance of Servlet" Watch "Explicitly instance of Servlet" New topic

Explicitly instance of Servlet

Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Can anybody tell me whether we can Explicitly create the instance of a Servlet.I mean
1)'Not by Calling Servlet first time or
2) Load on Startup'.

Does anybody know can we create an instance of Servlet without these options.

This question was asked to me in an interview.

[ June 11, 2004: Message edited by: Rahul Juneja ]

Rahul Juneja
ThoughtClicks -
Ivor Lithan

Joined: May 30, 2004
Posts: 10
Not quite sure what you mean, but if you mean what I think you do, you could do :

or am I missing what you are asking?
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Thanks Dear,

I was asking the same but one more Question which comes in my mind is that what is the practical implementation of this.

Can you please give me some idea about that.

Rahul Juneja
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Well, a servlet is just a Java class that happens to implement a particular interface. There's nothing to stop you (other than the potential for confusing a reader, and other issues of general good design) from giving it a "main" method, and/or a constructor, and creating an instance of it in the way you would any other Java object.

Of course, if you create an instance of a servlet class yourself, there will be no container that knows about it, so it will never receive HTTP requests, but I gues that's not what you want, is it?

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ivor Lithan

Joined: May 30, 2004
Posts: 10

Well, the one time I used that was when I had a servlet that implemented runnable, with the idea being I could have a thread running and then query it through the web front end - and this servlet was kicked off by a scheduler that I had written - and so it used that bit of code to start it all off - other than that I've never used it for anything
I agree. Here's the link:
subject: Explicitly instance of Servlet
Similar Threads
servlet & constructors
Servlet Instance
Calling Init and Destroy Explicitly
Servlet's constructor