File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init() and http related questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init() and http related questions" Watch "init() and http related questions" New topic
Author

init() and http related questions

osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
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...


SCJP/SCWCD
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
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

Joined: Aug 27, 2003
Posts: 1066
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

Joined: Jan 25, 2005
Posts: 78
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() and http related questions