How to write Servlet hello world Example

Here we will learn to create simple web application with Servlet sending Hello World to JSP page.

 Step 1 : Create Dynamic Web Application

First step would be to create a dynamic web application in Eclipse. To create this go to File >> New >> Other >> Dynamic Web Project. Provide the name of this project as “HelloWorldServlet“.

Step 2 : Create Servlet

Once application created next step would be to create Servlet which will handle the request. Create Servlet named “HelloWorld.java” in com.jbt package.
HelloWorld.java

package com.jbt;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public HelloWorld() {
	}

	/*
	 * This method will handle all GET request.
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("Hello World");
	}

	/*
	 * This method will handle all POST request
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}

Step 3 : Map URL to Servlet

Now you have created Servlet. Next step would be to map this servlet to an URL(/HelloWorld). You need to add an entry for this in web.xml(WebContent\WEB-INF).

web.xml

Here we will learn to create simple web application with Servlet sending Hello World to JSP page.

Step 1 : Create Dynamic Web Application

First step would be to create a dynamic web application in Eclipse. To create this go to <strong>File &gt;&gt; New &gt;&gt; Other &gt;&gt; Dynamic Web Project. </strong>Provide the name of this project as “<strong>HelloWorldServlet</strong>”.

Step 2 : Create Servlet

Once application created next step would be to create Servlet which will handle the request. Create Servlet named “<strong>HelloWorld.java</strong>” in com.jbt package.
HelloWorld.java

package com.jbt;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloWorld
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public HelloWorld() {
}

/*
* This method will handle all GET request.
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

System.out.println("Hello World");
}

/*
* This method will handle all POST request
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

}

}

Step 3 : Map URL to Servlet

Now you have created Servlet. Next step would be to map this servlet to an URL(/HelloWorld). You need to add an entry for this in <strong>web.xml(WebContent\WEB-INF).</strong>

web.xml

<?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_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>HelloWorldServlet</display-name>
<servlet>
 <description></description>
 <display-name>HelloWorld</display-name>
 <servlet-name>HelloWorld</servlet-name>
 <servlet-class>com.jbt.HelloWorld</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>HelloWorld</servlet-name>
 <url-pattern>/HelloWorld</url-pattern>
 </servlet-mapping>
</web-app>

Step 4 : Run Server and Test

Now everything is in place. So start the server and hit the URL (http://localhost:8080/HelloWorldServlet/HelloWorld).

And check the output in console. It should display “Hello World“. Till now we have not directed response to JSP so it will get displayed in Console only. In next article will display this output in JSP page.

4 Comments

Leave a 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.