Creating your first servlet

In this tutorial, we will learn how to create very basic web application with Servlet.

Servlet is a class that extends the capabilities of the servers to intercept and respond to incoming requests. Servlet is a web component that is written, built and deployed on the server to create dynamic web pages.

To start with, we need Eclipse IDE to be installed in our system and follow the steps as listed below.

1. Open Eclipse and create a Dynamic Web Project.


Servlets2. Specify the project details as shown below and click on Next.

Servlets

3. Set the Web Module details and generate the web.xml.

4. Click on Finish. Right-click on the Servlet Demo project and click on ‘Convert to Maven Project’.

Servlets

Thats it. We are done with the basic setup.

Validate the web.xml file looks like one mentioned below.

web.xml

Create the DemoServlet class in the same package as specified above in the web.xml.

DemoServlet.java

Now let’s create two JSP files – hello.jsp and test.jsp under Web Content.

hello.jsp

Test.jsp

Next compare and validate the pom.xml with the one mentioned below –

pom.xml

Now run the application on the server.

Run Servlets

Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.