- Servlets are Server side programs which is written in Java language.
- Deployed in a Web container of an application server which provides a runtime environment for servlets.
- Executed when the J2EE application server receives a client request that is passed to the Web container, which in turn invokes the servlet.
- A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model.
- Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
- Features of Java Servlets are:
- Security :- Inherits the security feature provided by the Web container.
- Session Management :- Maintains the identity and state of an end user across multiple requests.
- MultiThreading :- It is multithreaded.
- The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.
- All servlets must implement the Servlet interface, which defines life-cycle methods. [ init(), service(), destroy() ].
- When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API.
- The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
- The Servlet Class Hierarchy consists of two top level interfaces which are implemented by the GenericServlet class:
- javax.servlet.Servlet
- javax.servlet.ServletConfig
- The GenericServlet class is extended by the HttpServlet class which in turn is extended by a user defined class.
The Servlet Life Cycle includes:
-
init() :- Initialization of servlet instance using the init() method.
-
service() :- Servicing a client request using the service() method.
-
destroy() :- Destroying a servlet instance using the destroy() method.
In HttpServlet
The sequence in which the Web container calls the life cycle methods of a servlet are:
-
The Web container loads the servlet class and creates one or more instances of the servlet class.
-
The Web container invokes the init() method of the servlet instance during initialization of the servlet. The init() method is invoked only once in the servlet life cycle.
-
The Web container invokes the service() method to allow a servlet to process a client request.
-
The service() method processes the request and returns the response back to the Web container.
-
The servlet then waits to receive and process subsequent requests as explained in steps 3 and 4.
-
The Web container calls the destroy() method before removing the servlet instance from the service. The destroy() method is also invoked only once in a servlet life cycle.
Creating the Servlet involves:
-
Coding the Servlet :- Includes reading and processing a client request and sending the response back to the client.
-
Compiling the Servlet :- Include the servlet-api.jar file in the classpath and compile the servlet to generate a .class file.
-
Packaging the Servlet :- Creates the deployment descriptor that contains the configuration information about the Web application in which it resides. It packages the servlet in a WAR file and deploy it in the server.
-
Accessing the Servlet :- Calls the servlet from the client browser by typing the servlet’s URL in the address bar of the browser.
Note:- Consider Your Application is Hello.com carrying all the components and web related files.
Click here to download the code to test
Steps for Compiling your servlets.
Steps for Packaging and Deploying your servlet application..
Deploying Servlet on Tomcat Server
Starting Servlet Application
Running Servlet Application
Click here to download the code to test
Recent Comments