aspose file tools*
The moose likes Servlets and the fly likes Compiler servlet constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Compiler servlet constructor" Watch "Compiler servlet constructor" New topic
Author

Compiler servlet constructor

stefania ferrarelli
Greenhorn

Joined: Jul 26, 2009
Posts: 29
Hi to all,
I would understand exactly the steps for creating a servlet, because I have any doubts.
1-I write servlet.java
2-I compile and compiler makes servlet.class
3-I start container and it search for file.class
4-container loads servlet.class
5-compiler supplied default constructor-?- runs making and object, not a servlet
6-container calls init() on the object already made by compiler, makes the object became a servlet making a ServletConfig at the moment of the init() call - ? -
7-now the servlet exists and can service() requests making one thread per request.

My doubts are:
-the compiler default constructor makes an object before or after the container loads the servlet?
-why I never see on the code of the servlet the compiler supplied default no-arguments constructor?
-can I write an overloaded constructor(for instance with arguments) on a servlet(on netbeans IDE I never get a constructor when I make a servlet)?
-why initialization code is on init() and not on constructor()?
-container gives ServletConfig at the moment it calls init() or before calling it?
-why I find on netbeans the method processRequest by default, which I cannot find on javaxservlet or javax.servlet.http APIs?
-what makes the compiler supplied default constructor start creating an instance of servlet class?
-on SE I compile(javac) and later execute(java), what on EE corresponds to execution of a class(servlet)?Is it corresponding to the start of the container?Or to the request of a client?
Thank you
Stefania
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Some of these are generic Java questions rather than SErvlet questions, but essentially the constructor is part of the Java Class lifecycle, the init() method is part of the servlet lifecycle.
The constructor always gets called when creating a new class instance, a class is required before any method, including init() can be called.
The javax.* classes are part of JEE and not standard Java (JSE) , so you need to include their classes on your own.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

stefania ferrarelli wrote:
My doubts are:
-the compiler default constructor makes an object before or after the container loads the servlet?

After the Container loads the Servlet class, it(the Container) instantiates an instance of the Servlet.
stefania ferrarelli wrote:
-why I never see on the code of the servlet the compiler supplied default no-arguments constructor?

Did you find any default, compiler supplied class in other java classes?
stefania ferrarelli wrote:
-can I write an overloaded constructor(for instance with arguments) on a servlet(on netbeans IDE I never get a constructor when I make a servlet)?

Yes, Even you can write your own constructor with no arguments. because, when the Container instantiate a Servlet, it calls the no-args Constructor.
stefania ferrarelli wrote:
-why initialization code is on init() and not on constructor()?

Because, it's too early to give a ServletConfig to the instantiated Servlet instance within its Constructor.
stefania ferrarelli wrote:
-container gives ServletConfig at the moment it calls init() or before calling it?

Actually, there is two overloaded init() method, one takes ServletConfig as an argument, and the other is a no-arg method. The Container calls the argumented version method, and within that method, it calls the no-args method.
stefania ferrarelli wrote:
-why I find on netbeans the method processRequest by default, which I cannot find on javaxservlet or javax.servlet.http APIs?

It's a NetBeans supplied method, ultimately, it calls the process() method of the Servlet.
stefania ferrarelli wrote:
-what makes the compiler supplied default constructor start creating an instance of servlet class?

The Container.
stefania ferrarelli wrote:
-on SE I compile(javac) and later execute(java), what on EE corresponds to execution of a class(servlet)?Is it corresponding to the start of the container?Or to the request of a client?

At the start up of the Container, it deploys the web application, instantiate the Servlets, and serves on the request time.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 387
I am not agree with one point , we can provide our own constructor.

Which i have tried , compiler compile it safely, but container doesn't find and load that servlet as it is having no argument constructor provided by me not by the compiler.



even with argument constructor is also not allowed to declare in servlet.

please clarify my doubts.

while executing that servlet, getting error
HTTP Status 404 - Servlet testInterview is not available

one more query: Did you find any default, compiler supplied class in other java classes?


yes you are right , we didn't find and as default constructor provided by Java in every class.
but over there we are allowed to create own constructor also either no argument or argument constructor , but in case of servlet we are not able to provide any default or argument constructor..

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Prabhat Ranjan wrote:I am not agree with one point , we can provide our own constructor.

Sure you can, but what's the point?

Without a nullary constructor, the container can't create an instance of the servlet class. So is this a good idea? Just because you can do something, doesn't mean it's appropriate.

There is never a need for a servlet class to have a constructor as there is nothing you can do in a constructor that isn't more appropriate to do in the init() life-cycle method which is invoked after the container has had a chance to create the ServletConfig instance (which is usually essential to servlet setup).

Which i have tried , compiler compile it safely, but container doesn't find and load that servlet as it is having no argument constructor provided by me not by the compiler.


It has nothing to do with who provides the constructor. A constructor is a constructor. The problem with your servlet is that your ill-conceived constructor is not public.

But bottom line: it's bad form to use constructors in servlets. Use the life-cycle methods in the manner in which they were intended.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 387
thanks you are very correct to pointing me out. i missed to declare it as public.

Now i also got the point.

We can define constructor, but it does nothing.
Container calls implicit default constructor to load & instantiate the servlet which is meaning full.

init() method is so much useful to get reference of ServletConfig and servletContext.
and more to initialize resource.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiler servlet constructor
 
Similar Threads
choosing between init() and a constructor
Servlet Life Cycle Doubts
init() method in a Servlet's lifecycle.
init method of servlets
when ServletConfig object is created?