Servlet Java

 Java Servlets Introduction, HTTP Servlet Basics, The Servlet Lifecycle, Retrieving Information, Sending HTML Information, Session Tracking, Database Connectivity.

Servlets क्या है। - 

Java Servlets are programs that run on a web or application server and act as a middle layer between a request coming from a web browser or other HTTP client and database or applications on the HTTP server.
    Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.
     Java Servlets often server the same purpose as programs implemented using the Common Gateway Interface (CGI)

Advantages of Servlet Over CGI


Servlets often several advantages in comparison with the CGI.
  1. Performance is significantly better.
  2. Servlets execute within the address space of a Web server. it is not necessary to create a separate process to handle each client request.
  3. Servlets are platform-independent because they are written in java.
  4. Java security manager on the server enforcess a set of restrictions to protect the resources on a server machine. So servlets are trusted.
  5. The full functionality of the java class libraries is available to a servlets. It can communicate with applets, database, or other software via the sockets and RMI mechanisms that you have seen already.

Architecture Of Servlet


Following diagram shows the position of Servlets in a Web Application.




Servlets Tasks :-

  1. Read the explicit data sent by the clients (browsers). This includes an HTML from on a web page or it could also come from an applet or a custom HTTP client program.
  2. Read the implicit HTTP request data sent by the clients (browser). This includes cookies, media types and compression schemes the browser understands, and so forth.
  3. Process the data and generate the result. This process may require talking to a database, executing an RMI or COBRA call invoking a Web service, or computing the response directly.
  4. Send the explicit data the document to the clients (browser). This document can be sent in  a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
  5. Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (HTML), setting cookies and caching parameters, and other such tasks.

   Life Cycle Of a Servlet


  • Servlet class is loaded 
  • Servlet instance is created 
  • Init method is invoked
  • Service method is invoked
  • Destroy method is invoked 
The web container maintains the life cycle of a servlet instance. Lets see the life cycle of the servlet.
  • Servlet class is loaded 
  • Servlet instance is created 
  • Init method is invoked
  • Service method is invoked
  • Destroy method is invoked 
As displayed in the above diagram, there are three states of a servlet: now, ready and end, The servlet is in new state if servlet instance is created. After invoking the unit() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.




1. Servlet class is loaded -


         The classloader is responsible to load the instance of a servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

2. Servlet instance is created -

          The web container creates the instance is creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

3. Init method is invoked -

           The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:

Public void init (Servlet Config config) throws Servlet Exception 

4. Service method is invoked -

           The web container calls the service method each time when request for the servlet is received. If servlet is not intialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the sevice method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below :

Public void  service (Servlet Request, Servlet Response response)
Throws  Servlet Exception, IOException 

5. Destroy method is invoked - 

The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the servlet interface is given below:
Public void  destroy ()

No comments

Powered by Blogger.