Pages Navigation Menu

Coding is much easier than you think

Servlet Hello World Example in Eclipse IDE with Tomcat Server

 
Helloworld example using Servlet
 
In our previous tutorial we have learnt on how to create a Servlet with Eclipse IDE and Tomcat server, in this article we shall learn to run a sample hello world program using servlet to display welcome message in browser.
 

Library required

 
servlet-api-3.0.jar
 

Final Project Structure

 
HelloWorld in Servlet
 

Servlet

 

package com.servlet;

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;

public class HelloWorldServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
	          out.println("<html>");
  	          out.println("<head><title>Hello world in Servlet</title></head>");
   	          out.println("<body>");
	          out.println("<p>Hurray!! This Servlet worked!!!</p>");
	          out.println("</body>");
    	          out.println("</html>");
	}
}

 

web.xml

 
Having created a simple servlet, Now the question arises, how the container knows which servlet is being requested by the client, the answer to this question is web.xml file that resides there on server and it have valid mappings from request to servlet. This web.xml looks something like this:
 

<?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">

	<display-name>HelloWorldServlet</display-name>
	<servlet>
		<servlet-name>HelloWorld</servlet-name>
		<servlet-class>com.servlet.HelloWorldServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
</web-app>

 
Every servlet in a web application must have an entry into this file within two tags and . Now let’s see these fields in details.
 
<servlet-name>: A name is given to the servlet within this tag; name could be any user defined value.
<servlet-class>: Here we give the name with path to exact servlet class.
<url-pattern> : Here we give the url pattern of requested url, on which the servlet will be called.
 
No if we want to call a servlet for helloworld, then the requested url will be like http://localhost:8089/HelloWorldServlet/helloworld. Here / helloworld is the url-pattern for servlet.
 

Given below is the generalized url

http://<server>:<port>/<webapp>/urlpattern

 
So whenever this url is requested then it will search for a url-pattern with /helloworld in web.xml, now the servlet-name to this mapping will be compared to servlet block and required class file will be called according to servlet-class tag.

So this is how web containers maps a request from a client and invoke a right servlet and right method and the dynamic response is being served to the client.
 

Demo

 
Now right click on project [Root] > Run As > Run on Server
 
Run hello world servlet
 
See in the URL ‘http://localhost:8089/HelloWorldServlet/’ is our servlet application directory. Here I have got a 404 page since i have not configured any welcome page in my web.xml
 
Now if you try to access the project via http://localhost:8089/HelloWorldServlet/helloworld then the following output is obtained.
 
Run-hello-world-servlet2
 
Hope you understood how this servlet mapping occurs in java web application.
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.