Wednesday 23 February 2011

Servlets

Servlets are miniature programs that must be run inside another program called a container. The container is responsible for brokering communication between the servlet and the outside world.
Apart from that servlets are like every Java Application. They can import packages, write to the hard drive, and even install viruses on your machine. By default, servlets are trusted code. The reason is that if someone has enough permission on your computer to install a servlet, he already has enough power to tie the whole machine in knots anyway.

Servlets are not java appliactions. They must run inside a container instead of form a command line.
Servlets are not the only way for the server to communicate with a browser.  A web Server is an HTTP application. It receives communication via the HTTP protocol and it communicates with the browser via HTTP also. You can write your own HTTP server-side application that processes its own requests, bypassing both servlets and servlet container


HOW SERVLETS WORK:
Servlets are really parts of an application and require a servlet container to run. This servlet container is responsible for instantiating your servlet and calling the appropriate methods in the servlet at the appropriate times.

When you type the name of a servlet, you are really making a call to a program that is located on a server and not on your machine.
Figure below shows the process graphically:

How Servlet works
 The Servlet container is responsible for instantiating your servlets.
  • You type in the URL of a servlet that you want to call.
  • The browser creates a request that contains the servlet and name of you machine so that the server will know who to send feedback to.
  • The server receives the request and hands it to the servlet container. The servlet container is a program that knows how to run servlets.
  • The servlet container checks to see if any instances of this servlet are already in memory. if not, it loads an instance and run the servlet init() method.
  • The servlet container waits for the init() method to finish. it then calls the service() method. in your servlet from a new thread.
  • The service() method calls the doGet() or doPost() method depending on what the request type is.
  • A second user's browser requests that the same servlet be run on its behalf.
  • The servlet container notices that an instance of the servlet is already in memory. so, it creates a new thread and starts running the same servlet that you are running.
  • This new thread calls the service() method.
  • If this is an HTTP servlet, the service() method calls the doGet() or doPost() methods.
  • The first thread finishes and a response is sent to  the web server, which forwards it to your browser.
  • The second thread finishes and a response is sent to the web server, which forwards it to the second user's browser.
  • At a certain point in future, the servlet container decides to deinstantiate the servlet. At that point it calls the destroy() method once for each instance in memory
The servlet lifecycle is represented in following figure.
Servlet Lifecycle



No comments:

Post a Comment