• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Servlet Example

January 30, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • Java EE Tutorials
  • Servlets Tutorials
  • Servlets Interview Questions

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

Category: Java EETag: Servlets Tutorials

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.

Previous Post: « Generic Servlet and Http Servlet
Next Post: Web.xml »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact