Servlet Example

Let us create a simple servlet example using Eclipse IDE. To create servlet application we need to use following steps:

    • Environment used:
      • JDK
      • Eclipse IDE
      • Apache Tomcat Server
      • Java-api
    • Open the Eclipse IDE and Go to File Menu ->New->Select Dynamic Web Project.
      Servlet Example 1
    • Enter Project Name click on Next.
    • Click on Generate web.xml deployment descriptor and click Finish.
    • Now under the project, click on Java Resources ->select src, right mouse click and select New->Package and type the name as
      Servlet Example 2
    • Now click on the package and New ->select Servlet
      Servlet Example 3
      Type the class name as ServletDemo and click Finish.
    • Now to add servlet-api jar to project we need to do following steps.
      • Right mouse click on Project name, select -> Properties ->select Java Build Path->Click on Add External JAR’s.
      • Select servlet-api.jar from Apache Tomcat directory.
      • Click on Open and click OK.

      Servlet Example 4

    • Add the following content to the
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
       * Servlet implementation class ServletDemo
      public class ServletDemo extends HttpServlet {
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      	PrintWriter out = response.getWriter();
      	out.println("  <head>");
      	out.println("    <title>SimpleServlet</title>");
      	out.println("  </head>");
      	out.println("  <body>");
      	out.println("    Hello, World");
      	out.println("  </body>");
      	public void destroy(){
      	        System.out.println("shutting down....");

      Details of the above code:

      • doGet method is used to sent specific amount of data.
      • ServletRequest is used to collect data which is available with client requested data.
      • ServletResponse object is used to generate the output content.
      • ServletException is used to intimate container that request has generated error. IOException is used if input and output error is detected when servlet handles GET request.
      • destroy () is used to clean up the resources that servlet have initialized.
    • Edit the web.xml and include the following content to it:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
    • Select the class ServletDemo right mouse click->Run As and select Run on Server, an output as shown below should be achieved:

Servlet Example 5

Previous Tutorial : Generic Servlet and Http Servlet  || Next Tutorial : Web.xml

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!