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("");
  	          out.println("
Hello world in Servlet");
   	          out.println("");
	          out.println("

Hurray!! This Servlet worked!!!

"); out.println(""); out.println(""); } }

 

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:
 




	HelloWorldServlet
	
		HelloWorld
		com.servlet.HelloWorldServlet
	
	
		HelloWorld
		/Hello
	

 
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://://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.
 

One Comment

  1. Hi,

    I still get the above error…