Hello World Servlet

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

in below example I am going to create a servlet extending javax.servlet.http.HttpServlet class which is an abstract class.

Hello World Servlet Example :-

Step - 1

Create a class file inside the dynamic web project as shown in below :-

create-servlet-class

Step - 2

Complete Java code for Hello Servlet Class :-

hello-world

Step - 3

Mapping above servlet class inside the "web.xml" file as shown in below:-


servlet-mapping

Step - 4

Deploying the web project on tomcat server using eclipse


deploy-projectDeploy a web project

Step - 5

Accessing the servlet using below URI:-
http://localhost:444/agile-servlet/hello


accessing-servlet-url-pattern