• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization of ServletConfig and ServletContext objects

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using init(). Otherwise it your responsibility to call the superclass to initialize the servlet config, which you are not doing.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thank you guys.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Paul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic