Java

Creating a Simple HTTP Servlet

Programmingempire

In this article on Creating a Simple HTTP Servlet, I will explain writing the code of your first servlet using the Eclipse IDE. Basically, a servlet is nothing but a java class that serves the purpose of extending the capabilities of a server. Furthermore, a servlet handles the request and response operations.

Steps for Creating a Simple HTTP Servlet

The following steps should be followed for Creating a Simple HTTP Servlet.

At first, create a Dynamic Web Project in Eclipse as shown below.

Create a Dynamic Web Project
Create a Dynamic Web Project

After that, provide a name to the project, and click on the Finish button.

Dynamic Web Project
Dynamic Web Project

Now that, we have created our project, right-click on Java Resources -> New -> Servlet

Create a Servlet
Create a Servlet

Now provide a name to the servlet and click on Next.

Creating an HttpServlet
Creating an HttpServlet

Similarly, again click on Next and then select the checkbox for the doGet() method. Further, click on the Finish button.

Select the doGet() Method
Select the doGet() Method

Since now our servlet is created, we need to add the corresponding jar file. Therefore find the Project name and right-click on it to select the Build Path option. Further, select the Configure Build Path option.

Configure the Build Path
Configure the Build Path

Under the Libraries tab click on the Add External JARs button.

Add Libraries
Add Libraries

Now we need to find and select a specific jar file. It is called the servlet-api.jar file. As can be seen, we find the specific jar file from the lib folder as shown below.

Select the servlet-api.jar file
Select the specific jar file

Now you can see the servlet-api.jar file under the Libraries tab. Finally, click on the OK button.

Libraries in the Project
Libraries in the Project

Once, we have added the necessary libraries for our servlet, we can write the code for the servlet to display a simple web page. So, first import the java.io.* package as shown below.

Import the IO package of Java
Import the IO package of Java

In order to display a simple web page, call the setContentType() method using the implicit response object and pass “text/html” as the parameter. After that, create an object of the PrintWriter class. Further, call the write() method to enclose the HTML tags. Look at the following code.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public MyServlet() {
        super();
          }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.print("<html><head>My First Servlet</head><body>");
		out.print("<center><h2>");
		out.print("Welcome to HTTP Servlets</h2></center>");
		out.print("<p>doGet() Method is executing....</p>");
		out.print("</body></html>");
	}
}

Now run the servlet to get the following output.

An Example of Creating a Simple HTTP Servlet
An Example of Creating a Simple HTTP Servlet

programmingempire

You may also like...