This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes question in init method 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 "question in init method" Watch "question in init method" New topic
Author

question in init method

Deepak A
Ranch Hand

Joined: Oct 04, 2001
Posts: 120
Why do we need to do this mandatorily within a servlet.
public void init(config)
{
super.init(config);
}
Please do not tell me that we register it with superclass(Generic) servlet so that we can get the initialisation parameters etc etc...
I would look at having the same effect with :
public class myserv extends HttpServlet
{
ServletConfig scfg;
public void init()
{
this.scfg = config;
}
public void service()
{
scfg.getInitParameter();
}
}
whats wrong with the second code....


Face Off.
Ken Robinson
Ranch Hand

Joined: Dec 23, 2003
Posts: 101
Your Servlet extends HttpServlet. What happens in HttpServlet's init() method? Since it is not possible to know what is in that class today (maybe it is) or what will be there tomorrow (next spec is released), you are better off calling super.init() on any subclasses.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You say Please do not tell me that we register it with superclass(Generic) servlet so that we can get the initialisation parameters etc etc...
Why must we not tell you this? it is true!
Your code has some of the same effects as calling super.init(ServletConfig). However:
  • your code seems to be wrong, in that you override init, and not init(ServletConfig), so your assignment of cfg makes no sense.
  • your solution is more code than simply calling super.init!
  • neither your code nor other code which inherits from your class will get a sensible answer from calling getInitParameter(), getServletConfig() or any of the other methods which depend on having a valid config


  • Does that make sense?


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Deepak A
    Ranch Hand

    Joined: Oct 04, 2001
    Posts: 120
    Hi Kenneth/Frank,
    Here is my code :
    public class myserv extends HttpServlet
    {
    ServletConfig scfg;
    public void init(ServletConfig config)
    {
    this.scfg = config;
    }
    public void service()
    {
    scfg.getInitParameter();
    }
    }

    Now bout Frank's points :
    Frank says, "your code seems to be wrong, in that you override init, and not init(ServletConfig), so your assignment of cfg makes no sense."
    Now with the above use of init method ?
    Frank says, " your solution is more code than simply calling super.init! "
    Yes, thats true Frank there is more code involved, but is my code wrong ? If it is, what/why is it wrong ?
    Frank says, "neither your code nor other code which inherits from your class will get a sensible answer from calling getInitParameter(), getServletConfig() or any of the other methods which depend on having a valid config"
    I feel this point might answer my question.Can u ellaborate on this ?
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12671
        
        5
    It is wrong because it does not make correct use of the servlet container API. Thus you will not get correct results when using methods that ultimately depend on correct use of the servlet container.
    Bill


    Java Resources at www.wbrogden.com
    Deepak A
    Ranch Hand

    Joined: Oct 04, 2001
    Posts: 120
    William u mean to say,
    It is the servlet container that needs super.init(config) method in the init method for giving proper outputs ?
    So do i assume this is a standard any servlet container has to follow,in case it claims to b servlet compliant ?
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12671
        
        5
    Yes, that is exactly what I mean to say.
    I had hoped to give you a URL for the servlet API documentation in a nice convenient PDF document, but Sun seems to have re-arranged their web site AGAIN!
    Bill
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: question in init method
     
    Similar Threads
    init() and http related questions
    what's the benefit to add "synchronized" before "init" method?
    init(ServletConfig config) query
    why init() method is not final?
    why super.init() is used??