GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Can we write constructor for Servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Can we write constructor for Servlets?" Watch "Can we write constructor for Servlets?" New topic
Author

Can we write constructor for Servlets?

Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
Hi all,

I just got a basic doubt regarding constructors in servlet.
For writing servlets we are extending our servlet class to HttpServlet/GenericServlet, both classes are abstract. As per the specification they do have the constructor but it does nothing.

I wrote a simple servlet class and it runs fine.
Then I wrote a constructor in my servlet and got ClassNotFound exception followed by IllegalAccess exception during loading of it. It did not give me any compilation time error.

Is anywhere anything mentioned regarding constructors in servlet ???


Regards,<br />Samir (SCJP 1.4)
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Which type of constructor you had definded ? The app server load the servlet class using default constructor with no argument. It you defined constructor with arguments, the app server can not load the class.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
I just put the default no arg constructor.
And got the exception as I said before.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Does the constructor is defined as public ? What is coding inside the constructor?

In servlet you can define the constructor. I have no problem with constructor in my test servlet.

Thanks
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
Actually I removed the access modifier from the constructor so got the exception. It is running if I am putting the default constructor (same access modifier as class).
The exception I got was
Failed to load servlet: java.lang.ClassNotFoundException: class com.ssba.servlet.SimpleServlet : java.lang.IllegalAccessException: Class java.beans.Beans can not access a member of class com.ssba.servlet.SimpleServlet with modifiers ""

I just put a System.out.println() statement in the constructor.
I think we can't overload the constructor. In servlet it will always be the default one.

Thanks for the response...
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi All

I tried writing no-arg constructor for a servlet and it executed fine.

It is invoked before init method

My Doubt :

Then what is the purpose of having init method
(besides constructor can do whatever we wish while on loading)
Pranav Shukla
Ranch Hand

Joined: Oct 05, 2005
Posts: 64

My Doubt :

Then what is the purpose of having init method
(besides constructor can do whatever we wish while on loading)


The container first creates a new instance of Servlet (calls the constructor). Then it calls init(ServletConfig) method. If you are extending HttpServlet (most of the cases you will be doing that) you are indirectly extending GenericServlet which has the method init(ServletConfig) which stores the ServletConfig in an instance variable and then invokes parameterless init() method.

You have access to the ServletConfig object in your init() method ...You don't have access to the ServletConfig instance in your constructor. That is why we also have init() method.

Hope this answers your question..


Pranav Shukla
SCJP, SCWCD, SCBCD
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi Pranav

Thanks for ur explanation
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Samir,

Here container expects the servlet to have public no-arg constructor.Juz bcoz it does not have one, instantiation is getting failed.
It doesn't matter whether that constructor is supplied by you or the compiler.What matters is when the container tries to instantiate the class this public no-arg constructor should be there.

hth...

Regards,
Priya.
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
Thanks for your explanation Priya.

Now doubt is, if I will not provide the public no-arg constructor, then why it is not giving me the compilation error (in case of servlets).
Syntactically it may be correct, but as servlets are a special entity, I believe it would be better if it restricts during compilation.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi Samir,

If you do not provide no-arg constructor, compiler will provide the default. and it is fine to provide no-arg constructor if required ( Though it is not reqired in most of cases as Servlet provide init method). Finally Servlet is Java class and it must have constructor. As the container excepts public no-arg constructor, it must have implicit/explicit no-arg constructor.

Can you please put here your source code, what exactly you are trying.

Thanks
[ November 10, 2005: Message edited by: Narendra Dhande ]
Samir Dash
Greenhorn

Joined: Jun 08, 2005
Posts: 8
Hi Narendra,

You are right. I don't have any doubts in this now.
The source code will not be of much useful now.

Actually what I was trying to say is, assume we provide a no argument constructor without access modifier. In this case we will not get any compilation error (as theoritically the class is correct), but it will not be loaded by the container as there is no public no argument constructor existing. We will get error on loading of servlet. So if in case of servlets compiler can check this, it would be better.

Anyway we don't have any use of constructors in servlet as init() method is doing the purpose. I was just doing some testing with constructors...

Thanks for the explanation...
Dishi Jain
Ranch Hand

Joined: Jan 16, 2011
Posts: 46

Samir Dash wrote:Hi Narendra,

Actually what I was trying to say is, assume we provide a no argument constructor without access modifier. In this case we will not get any compilation error (as theoritically the class is correct), but it will not be loaded by the container as there is no public no argument constructor existing. We will get error on loading of servlet. So if in case of servlets compiler can check this, it would be better.




Having a constructor without any access modifiers mean, it is private. And so we do not allow any other class to create instance of servlet class by doing so. Hence the error is coming.
For those visiting the question for an answer and get one more doubt - like me!

Thanks,
Dish
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Dishi Jain wrote:
Having a constructor without any access modifiers mean, it is private....

Nope. It's default. Not private
If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

Read more about it here http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
GeeCON Prague 2014
 
subject: Can we write constructor for Servlets?