When creating a web based application using J2EE, we create our servlet classes.These servlet classes provide implementation for the methods (e.g- doGet, doPost, doPut etc) that are meant to process the requests send by the client , do appropriate processing on the requests and send the request back to the client.
But we do not bother to create an instance of our servlet class in our java code. Have you ever wondered how do the methods defined in our servlet classes get called ?
Let us look at the sequence of events that take place in processing a request sent by a client to the web server using a servlet class:
1.) We make a request (through a web browser) to a web server using HTTP.
2. )The request is received by the web browser.
3.) This request is forwarded by the web server to a Servlet Container. Now this Servlet container is really very imporatant !!!
Servlet container is the component of a web server that interacts with Java servlets and provides the network services over which requests and responses are sent, decodes MIME-based requests, and formats MIME-based responses.
An important point to note is that: all servlet containers must support HTTP as a protocol for requests and responses, but additional request/response-based protocols such as HTTPS (HTTP over SSL) may also be supported.
The Servlet container can be running in either of the following ways:
a.)running in the same process as local host Web Server
b.)running in a different process on tha same host Web Server
c.)running on a different host from the web Server for which
it processes the request.
4.) There can be more that one servlet class. Each servlet in the web application gets a servlet mapping. The url pattern is used to map URI to servlets. Based on the configuration of the servlets, the servlet container will invoke one of the Servlet and give the servlet Request and Response objects.
5.)The servlet container loads the servlet class using normal Java class loading facilities. After loading the Servlet class, the container instantiates it.
Therefore now we know that " instances of a servlet are created and managed by a servlet container."
BUT
Can we create an instance our servlet class?
The answer is yes !!!
Remember: We can create an object of our servlet class. But because servlet operation depends on the servlet context, request, response, etc etc provided by the container, there is nothing to be gained by creating one outside the container envrironment.
In one sentence - By doing so, we cannot expect to work as a servlet.
6.)After the servlet object is instantiated, the container initializes the servlet by calling the init method of the Servlet interface, before it can handle requests from clients.
7.) The servlet can now use the request object to find out various parameters that may have been sent with the request. For every request received to the servlet, the servlets service() method is called. For HttpServlet subclasses, one of the doGet(), doPost() etc. methods are typically called.
8.)The servlet will perform some logic for which it was programmed.
9.) Then the sevlet will send the data back to the client via a response object.
10.) The job of the servlect is finished.
Now the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state.
After the servlet container calls the destroy() method on a servlet instance, no other requests can be routed to to that instance of the servlet. Now the servlet instance is released by the servlet container and the servlet instance is available for garbage collection.
If the container needs to enable the servlet again, it will have to create a new instance of the servlet’s class
But we do not bother to create an instance of our servlet class in our java code. Have you ever wondered how do the methods defined in our servlet classes get called ?
Let us look at the sequence of events that take place in processing a request sent by a client to the web server using a servlet class:
1.) We make a request (through a web browser) to a web server using HTTP.
2. )The request is received by the web browser.
3.) This request is forwarded by the web server to a Servlet Container. Now this Servlet container is really very imporatant !!!
Servlet container is the component of a web server that interacts with Java servlets and provides the network services over which requests and responses are sent, decodes MIME-based requests, and formats MIME-based responses.
An important point to note is that: all servlet containers must support HTTP as a protocol for requests and responses, but additional request/response-based protocols such as HTTPS (HTTP over SSL) may also be supported.
The Servlet container can be running in either of the following ways:
a.)running in the same process as local host Web Server
b.)running in a different process on tha same host Web Server
c.)running on a different host from the web Server for which
it processes the request.
4.) There can be more that one servlet class. Each servlet in the web application gets a servlet mapping. The url pattern is used to map URI to servlets. Based on the configuration of the servlets, the servlet container will invoke one of the Servlet and give the servlet Request and Response objects.
5.)The servlet container loads the servlet class using normal Java class loading facilities. After loading the Servlet class, the container instantiates it.
Therefore now we know that " instances of a servlet are created and managed by a servlet container."
BUT
Can we create an instance our servlet class?
The answer is yes !!!
Remember: We can create an object of our servlet class. But because servlet operation depends on the servlet context, request, response, etc etc provided by the container, there is nothing to be gained by creating one outside the container envrironment.
In one sentence - By doing so, we cannot expect to work as a servlet.
6.)After the servlet object is instantiated, the container initializes the servlet by calling the init method of the Servlet interface, before it can handle requests from clients.
7.) The servlet can now use the request object to find out various parameters that may have been sent with the request. For every request received to the servlet, the servlets service() method is called. For HttpServlet subclasses, one of the doGet(), doPost() etc. methods are typically called.
8.)The servlet will perform some logic for which it was programmed.
9.) Then the sevlet will send the data back to the client via a response object.
10.) The job of the servlect is finished.
Now the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state.
After the servlet container calls the destroy() method on a servlet instance, no other requests can be routed to to that instance of the servlet. Now the servlet instance is released by the servlet container and the servlet instance is available for garbage collection.
If the container needs to enable the servlet again, it will have to create a new instance of the servlet’s class