How to do java servlet programs using tomcat!!

Are u confused how to do java servlet programs using Tomcat server.

Here you can learn how to do that!!

1.Download Tomcat if not installed  from here.

2.Run the application. If u have any user permission problems run it as Administrator.

3.While installing you should be care at one point.

  •  You have to give the path where the  java virtual machine is installed in your computer.
  • Generally it is installed at C:\Program Files \Java\jre7. You check it once.And copy this location and paste when installation wizard asks.Screen shot is as below.

4.After that choose the location where to install tomcat.By default it is stored at program files.

5.After installing open your browser(Firefox is recommended), type the address http://localhost:8080 at URL bar. Now you see Tomcat web page as shown below.

6.If you see this , it means that tomcat is installed correctly in your pc. Otherwise restart your pc and try it again.

Now you know how to install tomcat. We will see a simple java servlet program.

  1. write a java servlet program.
  2. Consider below program.

import javax.servlet.http.*;
import javax.servlet.*;

public class hello extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");

3.Copy this program , save it as ,compile and execute it.

4.Don’t know how to execute java programs on windows!!Go here!

5.When you execute a java program you will get a class file. So, now we will get hello.class file . This is very important for us now.

6.Now, go to tomcat folder i.e C:\Program Files >Apache Software Foundation>Tomcat 6.0>webapps>ROOT. Copy the WEB-INF folder.

7.Come to webapps.

8.Now create your own directory say helloworld. In helloworld directory paste the WEB_INF directory you have copied before. Now in WEB-INF directory  create a directory  called classes , if it is not there.

9.Now paste your hello.class in this classes directory.Now go to WEB-INF directory.

10.In WEB-INF directory you will see web.xml .Open it with any text editor.Delete any content present and paste the below code there.



11.Save it as web.xml itself.

12.Now open the browser and enter the address as

http://localhost:8080/helloworld/helloservlet .

13.You will see the ouput as “Hello. world!” which is the java servlet program.


  • Writing web.xml is the key point in this. The element between the tags <servlet-class></servlet-class> must be the .class file name.Here it is hello.class so it must be hello in between them.
  • <url-pattern> defines the url to be used. So whatever you used here is used in url bar.
    Eg: In our example it is /helloservlet. So the url is                               http://localhost:8080/helloworld/helloservlet.
  • We created helloworld directory. It must be in webapps directory.

How to access the html pages using tomcat??

1.It is very simple than the servlets.You might find it a lot easier.

2.What we do is create a html page.

3.For example write below code

<p>This is html page using tomcat server!!1<p>

4.Save it as index.html. Here it’s name must be index.html!!

5.Now paste this page into helloworld directory(NOT IN WEB-INF) which is located in webapps.

6.Now open browser and enter address as http://localhost:8080/helloworld.

7.Thats it.You will see the html page that we write just before.

This entry was posted in tech it !!. Bookmark the permalink.

One Response to How to do java servlet programs using tomcat!!

  1. Pingback: How to do JAVA programs on Windows?? | thegeekuday

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s