aspose file tools*
The moose likes Servlets and the fly likes Initialization of ServletConfig and ServletContext objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Initialization of ServletConfig and ServletContext objects" Watch "Initialization of ServletConfig and ServletContext objects" New topic
Author

Initialization of ServletConfig and ServletContext objects

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

As per Servlet Life cycle : servlet is loaded , instantiated and then initialized.

it means by the time init() is called ServletConfig object must have been initialized same is also true for ServletContext. object

then i have two issues with the following code




issue with part 1
why getServletConfig() retuen null ?


issue with part 2
If i uncommenet part 2 then it init() throws Exception , why ?



Note that if i replace the existing init(ServletConfig cfg) with init()then code run fines
with output as ServletConfig object is created ServletContext object is created.
why is that so ?



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

You should be using init(). Otherwise it your responsibility to call the superclass to initialize the servlet config, which you are not doing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


how can i do that since super class is abstract class ?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

As I said, you use init().

And, so what if its an abstract class? It still has methods, doesn't it?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Bear Bibeault wrote:

Otherwise it your responsibility to call the superclass to initialize the servlet config, which you are not doing.



yes of course init() can be called. but how superclass can be called to initialize the servlet config ?

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Tomcat source code to the rescue. The servlet API requires that the container call init(ServletConfig config), but the java.javax.servlet.GenericServlet provides:



This "convenience" method allows you to write an init() without the ServletConfig .

Bill
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


thank you guys.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

naveen yadav wrote:
yes of course init() can be called. but how superclass can be called to initialize the servlet config ?



naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

thank you Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization of ServletConfig and ServletContext objects