Creating a Servlet with Eclipse and Tomcat

ServletDefinition: What is Servlet

Java Servlets are server-side Java program modules that process and answer client requests and implement the servlet interface. It helps in enhancing Web server functionality with minimal overhead, maintenance and support.

A servlet acts as an intermediary between the client and the server. As servlet modules run on the server, they can receive and respond to requests made by the client.

A servlet is integrated with the Java language, it possesses all the Java features such as high portability, platform independence, and security and Java database connectivity.

Environment Used

  • JDK 7
  • Eclipse IDE
  • Apache Tomcat 6.x
  1. Java Installation

Java Installation Tutorial for instructions of how to install JDK 7.

2.  Tomcat Installation

Apache Tomcat Installation Tutorial for instructions of how to install Apache Tomcat.

After the installation test if Tomcat in correctly installed by opening a browser to http://localhost:8080/ .This should open an information page of Tomcat.               Afterwards stop Tomcat. Eclipse needs to start Tomcat itself for its deployments.

3.  Eclipse Installation

Eclipse Installation Tutorial for instructions of how to install Eclipse.

4.  Now we are ready to create Dynamic Web Project.

Creating Dynamic Web Project

To create a Servlet we need to create a new ‘Dynamic Web Project’. Follow following steps:

  • File menu -> New -> Dynamic Web Project

cre

  • Enter the project name as ‘HelloWorld‘ and make sure the Apache Tomcat v6.0 Target Runtime has been selected with the Dynamic web module version as 2.5 and click on ‘Finish‘ button.

Creating Servlet

  • Project -> New -> Servlet
  • Enter the Class name as HelloServlet
  • Click on ‘Finish‘

The complete source code for the class will now look like this:

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class HelloServlet

*/

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

* @see HttpServlet#HttpServlet()

*/

public HelloServlet()

{

super();

// TODO Auto-generated constructor stub

}

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

// TODO Auto-generated method stub

response.setContentType(“text/html”);

PrintWriter pw = response.getWriter();

pw.println(“<h1>Hello World</h1>”);

}

}

  • response.setContentType(); means we are setting what type of response we are sending back.
  • PrintWriter will be used to write the response, we can get the PrintWriter object from the response object.

Run the Servlet

  • Right click on the ‘HelloServlet.java’ Dynamic Web project -> Run As -> Run on Server. Select the existing ‘Tomcat v6.0 Server at localhost’ and click Finish.

http://localhost:8080/HelloWord/HelloServlet

ds

Note:The information provided here is best of my knowledge and experience if at all any modifications are to be made please help me with ur valuable suggestion which are always welcome…. 🙂

 

Leave a comment