Submitted by K.Priyadarsini N.Pandimeena V.Sarmila IIM.SC(CS&IT) Nadar saraswathi college of arts & science , Theni.
SERVLET:  Servlets provide a component-based, platform- independent method for building Web based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.  Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.  Performance is significantly better.  Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.  Servlets are platform independent because they are written in Java.  Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.
Servlet interface between client and server:
CGI(Common Gateway Interface): CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.
Advantage of servlet:
 Better performance: Because it creates a thread for each request not process.  Portability: Because it uses java language.  Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc.  Secure: Because it uses java language..
Website:  Website is a collection of related web pages that may contain text, images, audio and video. The first page of a website is called home page. Each website has specific internet address (URL) that you need to enter in your browser to access a website.  Website is hosted on one or more servers and can be accessed by visiting its homepage using a computer network. A website is managed by its owner that can be an individual, company or an organization.
HTTP:  The Hypertext Transfer Protocol (HTTP) is application-level protocol for collaborative, distributed, hypermedia information systems. It is the data communication protocol used to establish communication between client and server.  HTTP is TCP/IP based communication protocol, which is used to deliver the data like image files, query results, HTML files etc on the World Wide Web (WWW) with the default port is TCP 80. It provides the standardized way for computers to communicate with each other.
The Basic Architecture of HTTP:
Servlet API:  The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet API.  The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.  The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
javax.servlet: The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container. javax.servlet.http: The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.
Classes in javax.servlet package: Generic Servlet Servlet Input Stream Servlet Output Stream Servlet Request Wrapper Servlet Response Wrapper Servlet Request Event Servlet Context Event Servlet Request Attribute Event Servlet Context Attribute Event Servlet Exception Unavailable Exception
Classes in javax.servlet.http package: Http Servlet Cookie Http Servlet Request Wrapper Http Servlet Response Wrapper Http Session Event Http Session Binding Event Http Utils (deprecated now)
Http Servlet class:  The Http Servlet class extends the Generic Servlet class and implements Serializable interface. It provides http specific methods such as doGet , doPost , doHead , doTrace etc.
Life Cycle of a Servlet (Servlet Life Cycle):  The web container maintains the life cycle of a servlet instance. Let's 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.
Diagram of servlet life cycle:
 As displayed in the above diagram, there are three states of a servlet: New Ready End  The servlet is in new state if servlet instance is created. After invoking the init() 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.
Written inside the public service method: The public service method converts the Servlet Request object into the Http Servlet Request type and Servlet Response object into the Http Servlet Response type. Then, calls the service method passing these objects.
PROGRAM: public void service(Servlet Request req , Servlet Response res) throws Servlet Exception, IOException { Http Servlet Request request; Http Servlet Response response; try { request = (Http Servlet Request)req; response = (Http Servlet Response)res; } catch(Class Cast Exception e) { throw new Servlet Exception("non HTTP request or response"); } service(request, response); }
Written inside the protected service method: The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:
PROGRAM: protected void service(HttpServletRequest req, HttpServlet Response resp) throws ServletException, IOException { String method = req . getMethod(); if(method . equals("GET")) { long lastModified = getLastModified(req); if(lastModified == -1L) { } .... } }
Servlet in java , java servlet , servlet servlet and CGI, API

Servlet in java , java servlet , servlet servlet and CGI, API

  • 2.
  • 3.
    SERVLET:  Servlets providea component-based, platform- independent method for building Web based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.  Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
  • 4.
    Java Servlets oftenserve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI.  Performance is significantly better.  Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.  Servlets are platform independent because they are written in Java.  Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.
  • 5.
    Servlet interface betweenclient and server:
  • 6.
    CGI(Common Gateway Interface): CGItechnology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.
  • 7.
  • 8.
     Better performance: Becauseit creates a thread for each request not process.  Portability: Because it uses java language.  Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc.  Secure: Because it uses java language..
  • 9.
    Website:  Website isa collection of related web pages that may contain text, images, audio and video. The first page of a website is called home page. Each website has specific internet address (URL) that you need to enter in your browser to access a website.  Website is hosted on one or more servers and can be accessed by visiting its homepage using a computer network. A website is managed by its owner that can be an individual, company or an organization.
  • 10.
    HTTP:  The HypertextTransfer Protocol (HTTP) is application-level protocol for collaborative, distributed, hypermedia information systems. It is the data communication protocol used to establish communication between client and server.  HTTP is TCP/IP based communication protocol, which is used to deliver the data like image files, query results, HTML files etc on the World Wide Web (WWW) with the default port is TCP 80. It provides the standardized way for computers to communicate with each other.
  • 11.
  • 12.
    Servlet API:  Thejavax.servlet and javax.servlet.http packages represent interfaces and classes for servlet API.  The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.  The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
  • 13.
    javax.servlet: The javax.servlet packagecontains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container. javax.servlet.http: The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.
  • 14.
    Classes in javax.servletpackage: Generic Servlet Servlet Input Stream Servlet Output Stream Servlet Request Wrapper Servlet Response Wrapper Servlet Request Event Servlet Context Event Servlet Request Attribute Event Servlet Context Attribute Event Servlet Exception Unavailable Exception
  • 15.
    Classes in javax.servlet.httppackage: Http Servlet Cookie Http Servlet Request Wrapper Http Servlet Response Wrapper Http Session Event Http Session Binding Event Http Utils (deprecated now)
  • 16.
    Http Servlet class: The Http Servlet class extends the Generic Servlet class and implements Serializable interface. It provides http specific methods such as doGet , doPost , doHead , doTrace etc.
  • 17.
    Life Cycle ofa Servlet (Servlet Life Cycle):  The web container maintains the life cycle of a servlet instance. Let's 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.
  • 18.
    Diagram of servletlife cycle:
  • 19.
     As displayedin the above diagram, there are three states of a servlet: New Ready End  The servlet is in new state if servlet instance is created. After invoking the init() 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.
  • 20.
    Written inside thepublic service method: The public service method converts the Servlet Request object into the Http Servlet Request type and Servlet Response object into the Http Servlet Response type. Then, calls the service method passing these objects.
  • 21.
    PROGRAM: public void service(ServletRequest req , Servlet Response res) throws Servlet Exception, IOException { Http Servlet Request request; Http Servlet Response response; try { request = (Http Servlet Request)req; response = (Http Servlet Response)res; } catch(Class Cast Exception e) { throw new Servlet Exception("non HTTP request or response"); } service(request, response); }
  • 22.
    Written inside theprotected service method: The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:
  • 23.
    PROGRAM: protected void service(HttpServletRequestreq, HttpServlet Response resp) throws ServletException, IOException { String method = req . getMethod(); if(method . equals("GET")) { long lastModified = getLastModified(req); if(lastModified == -1L) { } .... } }