0

Out of curiosity I was looking at the HttpServlet class's code and found that its parent class "GenericServlet" defines the method "getServletName()" declared in the interface "ServletConfig". However the GenericServlet's getServletName() method makes a call to "sc.getServletName()" if the ServletConfig's object "sc" is not null. I could not understand how this thing works since it seems to be calling itself when I do a ctrl+click in eclipse to see the implementation of the method! There is no overridden implementation in the HttpServlet class too!

Here is a snapshot of the GenericServlet's implementation :

public String getServletName() {
    ServletConfig sc = getServletConfig();
    if (sc == null) {
        throw new IllegalStateException(
            lStrings.getString("err.servlet_config_not_initialized"));
    }

    return sc.getServletName();
}

Can anybody enlighten me on this..

  • 1
    It is not calling it self. The GenericServlet is calling servletConfig.getServletName() not this.getServletName(). It happens that GenericServlet for ease implement also ServletConfig interface, but it isn't servletConfig. It is getting serveltConfig object via it's init(ServletConfig config) method. And then in the getServletName() method it is just acting as a proxy to pass this call to the config.getServletName() method. What you don't understand here?? – Gas Sep 25 '14 at 13:50
  • Yes you are right @Gas but what I am not getting is how is it returning the servlet's name since ServletConfig's getServletName() is just an abstract method. Also if we ctrl+click on "sc.getServletName()" in GenericServlet class to see its implementation it would take us again to the same method! I want to know from where is the String (Servlet's name) is returned in essence.. – Shubhankit Roy Sep 26 '14 at 04:37

1 Answers1

5

javax.servlet.GenericServlet implements the ServletConfig interface but it does not contain the actual implementation for ServletConfig.It uses delegation by using config object, which is provided by the container while invoking the init method.

GenericServlet takes ServletConfig object (which is a StandardWrapperFacade obj for tomcat ) as a parameter for init(ServletConfig config) method and store its reference to the instance variable config when invoked by container.

  • Init method

     public void init(ServletConfig config) throws ServletException {
     this.config = config;//assign config to the instance variable
     this.init();
     }
    
  • getServletName method

    public String getServletName() {
       ServletConfig sc = getServletConfig();//returns the ref of instance variable i.e. config
        if (sc == null) {
            throw new IllegalStateException(
               lStrings.getString("err.servlet_config_not_initialized"));
        }
    
        return sc.getServletName();//call method on config object
      }
     }
    


    Therefor it's not calling the getServletName() on current instance( this) instead it's calling it on config object which is passed by the servlet container while initialing the servlet.


You should also look at the servlet Life-Cycle.

For tomcat org.apache.catalina.core.StandardWrapper provides the actual implementation of ServletConfig interface.

UPDATE:-

If you want to get the name of underlying class then you could use object.getClass().getName(); method.

vikrant singh
  • 2,091
  • 1
  • 12
  • 16
  • 1
    Thanks @Vikrant I was also thinking the same that the actual implementation must be somewhere else.. and as you cleared, its in org.apache.catalina.core.StandardWrapper for Tomcat where it makes another call to getName() to return the name. :) I guess previously my question was misunderstood that the call was recursive, I said so because on doing ctrl+click in eclipse on sc.getServletName() it took me to the same method! Anyways, thanks :) – Shubhankit Roy Sep 26 '14 at 08:28