Go to Home Page


Servlet Hands On


The servlet can be created using

We mostly used HttpServlet class, it provides http request specific methods

Directory Structure

Defines where need to put different types of file so that web container get information.

Directory Structure

Ref: https://www.cis.upenn.edu/~matuszek/cit597-2006/Pages/file_structures.html

The Below Example based on creating servlet without any IDE Refer Project Folder


Servlet Creation

Example Code Here Hello.java

Compiling & Deploying

To compile servlet different server provide different jar files

Compile & load class path

Create Deployment Descriptor

More Detalis on Deployment Descriptor

Deploying


You can use eclipse or any other IDE to do above things easily, you can refer Basic Servlet Project in folder using eclipse IDE

Eclipse Sample Servlet Project


How Servlet Works

As we studied in Servlet Life Cycle if the server requested first time web container load the servlet class, instantiate it, call the init method, other wise call the service method directly. The web container call the destroy method when it need to remove the servlet (time of stopping server, undeploy project)

WebContainer Handling Request

Ref: Src code of HttpServlet.java

Tips - to extract war file use jar -xvf warproject.war


Go to Home Page