File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we write constructor for Servlets?

 
Samir Dash
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ???
 
Narendra Dhande
Ranch Hand
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Samir Dash
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just put the default no arg constructor.
And got the exception as I said before.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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..
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pranav

Thanks for ur explanation
 
Priya Jothi
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 951
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic