Free Jobs

Java Forums: Core Java (15) J2EE (3) EJB (5) Servlet (3) JDBC (6) Hibernate (2) Struts (1) Swing (1) Oracle (7) JSP (4) Other (10)

Topic: Creating a simple Hello World Servlet using Tomcat Apache Server

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share

User: dhiraj5079 Post Date: 14 Sep 2010 03:30

A Java servlet is a J2EE component that run on the Server. In other words A Java servlet runs in a Web server or application server and provides server-side processing like accessing a database and/or e-commerce transactions etc.

Main Content:
Below are the steps to create first hello world servlet application.

a)Install jdk 1.6 from
b)Install Tomcat 6.0 from

c)set two environment variable as described below after checking that below is the right path
on your machine for Tomcat and JDK installation .If path is different then change the right of : accordingly.

APACHE_HOME : C:\apache-tomcat-6.0.29
JAVA_HOME : C:\Program Files\Java\jdk1.6.0_21

Below is the step to set environment variable:

For Windows XP: Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables -> System variables -> New

Enter the name and value for APACHE_HOME and JAVA_HOME.

This is just like giving a name to the above path so that we can access them quicly by that name.

d)Set the system variable for javac.Otherwise you will get error like 'javac' is not recognized as an internal or external command.

To set the javac command path in windows

Go to Start->Control Panel->System-->Advanced system settings

Click the "advanced' tab at the top, and then click the "environment variables" button at the bottom.

In the lower "system Variables" box, scroll through and highlight the "path" line.

Click the "edit" button.

Add the following (The first character is a semi-colon)
Caution:Do not delete whatever is the value of path is already there.
Just add the following value at the end.
;c:\Program Files\java\jdk1.6.0_06\bin at the end of value of the path.

After doing the above the command "javac" should now work from any directory.


Create a new text file named and save it as '' in the
C:\apache-tomcat-6.0.29\webapps\TestFolder\WEB-INF\classes\com\servlets folder.

Then copy and paste the following text into the '' file we created earlier
package com.MyFolder.servlets; import; import; import java.util.Date; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<xmp><html><head>"); out.println("<title>MyServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>Today's Date: " +new Date().toString() + "</p>"); out.println("</body></html>"); out.close(); } }
f)After that Go to Start-->cmd and change the directory to


Then use javac -cp %APACHE_HOME%\lib\servlet-api.jar
to create a class MyServlet.class in the same folder as

g)then browse to the folder C:\apache-tomcat-6.0.29\webapps\TestFolder\WEB-INF

Create a file named web.xml and copy paste the below content in the file.




This file is called Deployment Descripter and is used for deployment of the servlet.

h)Now to test your servlet
Start the apache server by Start-->CMD-->C:\apache-tomcat-6.0.29\bin\startup.bat
Then Browse the URL : http://localhost:1977/MyFolder/MyServlet

Note : Here we assume that your TomCat is cofigured for port 1977
If it is not then go to below file to change/Check the port.

Thus a simple servlet can be created very easily as described above.
You can add more functionality as per your requirement to file

About Writer:
Hi,I am IT Professional From Mumbai

Writer's Link: http://

Writer's Email:

  • User: mohit555 Post Date:   27 Mar 2011 10:32
    Good Article for Java Starters

Post Comment

Similar Articles Top ArticlesLatest Articles Other Articles by this Author Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)