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 javabeat.net.servlets
      Servlet Example 2
    • Now click on the package javabeat.net.servlets 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 ServletDemo.java:
      package javabeat.net.servlets;
      
      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 ServletDemo
       */
      public class ServletDemo extends HttpServlet {
      
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      	response.setContentType("text/html");
      	PrintWriter out = response.getWriter();
      
      	out.println("<html>");
      	out.println("  <head>");
      	out.println("    <title>SimpleServlet</title>");
      	out.println("  </head>");
      	out.println("  <body>");
      	out.println("    Hello, World");
      	out.println("  </body>");
      	out.println("</html>");
      	}
      	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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <servlet>
              <servlet-name>ServletDemo</servlet-name>
              <servlet-class>javabeat.net.servlets.ServletDemo</servlet-class>
          </servlet>
      
          <servlet-mapping>
              <servlet-name>ServletDemo</servlet-name>
              <url-pattern>/ServletDemo</url-pattern>
          </servlet-mapping>
      </web-app>
      
    • 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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. What are the difference between HttpServlet & Generic Servlets. Please provide one real time examples when to use this.

Speak Your Mind

*