• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

init() and http related questions

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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...
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why do we have two init methods?
A good question.

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:

1. init()
2. init(ServletConfig config) method.

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!

If there was only one init(ServletConfig config) method provided in GenericServlet then, our code init method would like this:

void init(ServletConfig config) {
super.init(config);
.....put our initialization code.....
}

Does that make any sense or did I confuse you further?
[ May 19, 2005: Message edited by: Vishwa Kumba ]
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your second question, there is a blank line between the section containing the headers and the body(payload).

A HTTP message(both request/response) are of this format:

1. an initial line,
2. zero or more header lines,
3. a blank line (i.e. a CRLF by itself), and
4. an optional message body (e.g. a file, or query data, or query output).

If an HTTP message includes a body, there are usually header lines in the message that describe the body.

The Content-Type: header gives the MIME-type of the data in the body, such as text/html or image/gif.
The Content-Length: header gives the number of bytes in the body.

Sample HTTP Exchange:



The above example is from this site:http://www.jmarshall.com/easy/http/
[ May 19, 2005: Message edited by: Vishwa Kumba ]
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic