my dog learned polymorphism
The moose likes Servlets and the fly likes Servlet Request Listener Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Request Listener Query" Watch "Servlet Request Listener Query" New topic

Servlet Request Listener Query

Reema Patel
Ranch Hand

Joined: Jan 26, 2006
Posts: 169
Hi ranchers,
Pls. consider the following code snippet taken from a tech web site.
I have two queries here:
(1) Is it a must to define the no-args default constructor explicitly? What's the harm if we rely on the Java compiler provided default constructor?

(2) Why do we need a static variable here. We know that there's just one copy of any static variable irrespective of inatances created. But, I wonder if there would be more than one instance of ReqListener class in any scenario.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

1) No there is no harm in relying on the default, no-arg constructor.
Remember though, the default constructor is only created if no others exist.
If you create any other constructors, then you must, if you need one, explicitly create the no-arg constructor as well.

2) The amount of code posted is not enough for us to say for sure that only one instance of that listener is going to be created.
[ February 28, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Reema Patel
Ranch Hand

Joined: Jan 26, 2006
Posts: 169
Thanks Ben for the post.
Here's the complete code.

And just one <listener> tag in web.xml

I don't think we need static variable out there?
Raghavendra Nittur

Joined: Feb 19, 2007
Posts: 29
Anyone know the anwer for reema's Query?

Please Let me know.


did you get the answer if so post it. am in need of the same.

Regards, Raghav
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
I think static is not needed here. There should be only one instance of this listener.

Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
I agree. Here's the link:
subject: Servlet Request Listener Query
jQuery in Action, 3rd edition