init() and http related questions

I want to ask the need for two init methods; one with no parameters, and one which takes the ServletConfig object. The methods that we call through the ServletConfig(getInitParameter, getServletContext) can be called directly through the GenericServlet methods. Also we have the getServletConfig method which returns the ServletConfig object for this servlet.

So why do we have two init methods??

Also while I study for the exam, I was confused about the following: How do the server and the client(browser) understand when the headers ended and the payload started through the connection? They can understand the end of the payload as they knw the content-length for the payload? But what about the headers? Where does the payload start?

I will greatly appreciate your comments and answers. Thanks in advance. best regards...

Servlet interface defines only 1 init method. This init method takes ServletConfig object as a parameter.(void init(ServletConfig config)) When it loads a new servlet for the first time, the Container creates this Servlet and calls the init() method for initialization passing it the ServletConfig object which the servlet can store and use it later. (ServletConfig contains useful information for the servlet, like the initialization parameters of the servlet mentioned in the DD web.xml etc.....).

But our friendly Generic Servlet implements the Servlet interface and provides us with 2 methods:

The implementation/code also for both the above 2 methods has been provided by GenericServlet!

1. init() method is a blank method. 2. init(ServletConfig config) method stores the ServletConfig object in the Servlet so that we could retrieve it later when we call getServletConfig(). It then calls the other init() method which is the blank method.

Now in our Servlet class which extends HttpServlet, if we wish to do some initialization, we just override init()

void init() { .....put our initialization code..... }

The Container after loading the Servlet for the first time, always calls the init(ServletConfig) method. Since we wouldn't have implemented the init(ServletConfig) method, the GenericServlet's implementation of the method gets called which in turn calls our method init() method. So this is a just a covenience method!

your answers were great, thank you. So, init(servletconfig) is called by the container. IF we override init(servletconfig) and does not include super(config), then getServletConfig in our doPost returns null. This is OK?

Also, i did not sniff the traffic between my client and the server but according to the 'netstat' results, I guess after the response is totally sent from the server, the server does not close the socket(write side) immediately? But i should verify it through sniffing.

regards.

Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066

posted May 20, 2005 03:09:00

0

So, init(servletconfig) is called by the container. IF we override init(servletconfig) and does not include super(config), then getServletConfig in our doPost returns null. This is OK?

Yes. I guess so, but haven't tried it.

Also, i did not sniff the traffic between my client and the server but according to the 'netstat' results, I guess after the response is totally sent from the server, the server does not close the socket(write side) immediately? But i should verify it through sniffing.

In theory, the socket is supposed to be closed after every request/response. But the server and IE explorer might do something smart for performance reasons which might be very vendor-specific or OS specific and nothing to do with the HTTP protocol.

For the SCWCD exam, it would be OK to accept that socket is closed after every request/response.