Freely Scripted Soul

November 18, 2007

Using Servlets in Ubuntu Gutsy Gibbon

Filed under: Ubuntu — Roshan @ 3:48 pm
Tags: , , , , ,

All commands and descriptions are CASE SENSITIVE






First of all, install Java6, Apache, Tomcat5.5 and all related packages:


Use Synaptic Manager(recommended)


Run these following commands at the terminal:

$ sudo apt-get install apache2-mpm-prefork apache2-common apache2-utils

$ sudo apt-get install sun-java6-jdk tomcat5.5 tomcat5.5-admin tomcat5.5-webapps

$ sudo apt-get install libapache2-mod-jk






Run this command at the terminal:

$ sudo a2enmod

Then a prompt for a module comes. There type “jk” and enter:

Module name? jk

Prompt comes back.

Edit the “jk.load” file using the following command:

$ sudo gedit /etc/apache2/mods-enabled/jk.load

A file will open with the following line in it:

LoadModule jk_module /usr/lib/apache2/modules/

Append the following lines in it:

JkWorkersFile /etc/apache2/

JkLogFile /var/log/apache2/mod_jk.log

JkLogLevel debug

JkLogStampFormat “[%a %b %d %H:%M:%S %Y] “

JkMount /jsp-examples worker1

JkMount /jsp-examples/* worker1

JkMount /servlets-examples worker1

JkMount /servlets-examples/* worker1

Save and close the file.

Edit the “” file using the following command:

$ sudo gedit /etc/apache2/

An empty file will open. There paste these contents:








Save and close the file.

Edit the “environment” file using the following command:

$ sudo gedit /etc/environment

A file will open with the following lines in it:




Append the following lines in it:



Save and close the file.

Edit the “.bashrc” file using the following command:

$ gedit $HOME/.bashrc

A file will open with a number of lines in it. Append the following lines in it:

export JAVA_HOME=/usr/lib/jvm/java-6-sun

export CATALINA_HOME=/usr/share/tomcat5.5

export CLASSPATH=/usr/share/tomcat5.5/common/lib/servlet-api.jar


Save and close the file.

Download this web.xml file to your HOME folder(or change your directory to the folder in which you have stored this downloaded file), rename it as “web.xml” and run this command at the terminal:

$ sudo cp web.xml /etc/tomcat5.5/

Setup is done.






Restart Apache and Tomcat by running the following commands at the terminal:

$ sudo /etc/init.d/apache2 stop

$ sudo /etc/init.d/tomcat5.5 stop

$ sudo /etc/init.d/tomcat5.5 start

$ sudo /etc/init.d/apache2 start

Restart the system for the changes to take effect.






To test whether Tomcat has been set up properly, open this link in the browser:


If you get the Apache Tomcat Homepage, it means that Apache and Tomcat has been set up properly.

To check whether Servlets has been set up properly, open this link in the browser:


It opens the Servlets examples page of Apache Tomcat. Run the examples on the page. If they execute properly, Servlets have been set up properly.

Copy the source code of the example “Hello World” there. Open a file in your home folder:

$ gedit

Paste the contents of the servlet-example in this file. Save and close the file.

Compile this file:

$ javac

If it doesn’t give any errors and the byte-compiled “.class” file is created, then Java has been set up properly.






Run this command at the terminal:

$ sudo mkdir /usr/share/tomcat5.5-webapps/ROOT/WEB-INF/classes/

(If it already exists, no problem!)

Store all your “.java” and “.class” files this “classes” folder.

For example:

$ cd /usr/share/tomcat5.5-webapps/ROOT/WEB-INF/classes/

$ sudo gedit

PS: It is a MUST to use “sudo” before each command as this folder is “writable” only with administrative privileges(Instead of typing it each time, you can also use the super-user shell by running the command “su”)

A file will open. Write your Servlet code in it. Save and close the file.

$ sudo javac

Now to run this Servlet, open this link in your browser:


This should work provided your Servlet code is right.

Now, suppose you want to write a HTML file which will use your Servlet program. Then you need to store it in your “ROOT” folder of Tomcat.

For example:

$ cd /usr/share/tomcat5.5-webapps/ROOT

$ sudo gedit HelloWorld.html

A file will open. Write your HTML code in it, which will interact with the “HelloWorld.class” file in the “classes” folder. Save and close the file.

Open this link in your browser:


This should work provided your HTML code is right and provided your Servlet code in the “classes” folder has been byte-compiled(“.class” file).



Web.xml –




PS: If something doesnt work, please check for missing or erroneous case-sensitive letters or slashes. If you face some problem and are unable to find a solution, feel free to add a comment here or mail me. I ll do my level best to find a solution.



PPS: Thanks to Madhusudan C S for the wonderful research 🙂




  1. I am a novice with Ubuntu and Linux so I need tons of information that I can digest easily. I am also of an older generation going towards 50 springs, so I tend to forget easily which has the consequence that I must repeat both readings and steps taken over and over again. Most of the Tutorial sites take for granted certain fore-knowledge of terms, expressions and steps to be taken. Thats a huge disadvantage for me because I have to spend so much time cross-referring to the fore-mentioned before I can proceed. One step forward and two steps backward…heheheheee : ) However I really admire your site where you give precise steps to take and what to expect. Thank you very much. I’m having trouble following all your steps but i’m sure I’ll get there eventually!!! KEEP UP THE FANTASTIC WORK, PLEASE!!!

    Comment by ANWAR KEMAL — January 12, 2008 @ 10:19 pm | Reply

  2. anwar, its my pleasure 🙂
    it feels great to know that my site is being helpful to others.. i ll try to keep it up..

    Comment by Roshan — January 14, 2008 @ 3:00 pm | Reply

  3. your link to web.xml goes to a very strange place. ill try to figure it out, but maybe you should look into hosting it somewhere less confusing?

    Comment by me — January 26, 2008 @ 10:01 pm | Reply

  4. i figured out your crazy place where you stored web.xml
    here is another place where i mirrored it, which does not require bug-me-not registration

    Comment by me — January 26, 2008 @ 10:12 pm | Reply

  5. ya.. esnips sometimes gives a prob..
    this link may work better
    this is the folder in which the web.xml files are stored..

    Comment by Roshan — January 27, 2008 @ 7:02 am | Reply

  6. This tutorial was interesting because you spent some time showing how to connect Tomcat with Apache via the modjk . . . then, you never gave an example of accessing tomcat through apache. Rather, your final “Using” example accesses a servlet through tomcat alone – as a standalone server. (http://localhost:8180/HelloWorld.html)

    If that was all you were explaining, you could have cut out most of the steps as using tomcat as a standalone server is easy.

    If you actually meant to explain how to access tomcat via apache, you stopped short of the most important part – an example.

    Comment by metoo — February 13, 2008 @ 4:09 am | Reply

  7. thanks for the guide Roshan. i’m really new to this stuff, your step by step guide is really helpful.

    to metoo:
    just goto http://localhost/servlets-examples or http://localhost/jsp-examples to verify the mod_jk install is working.

    Comment by anonymous coward — February 20, 2008 @ 1:58 am | Reply

  8. I must have spent more than 10 hours trying to get Apache Tomcat 5.5 and Apache to run side by side on Ubuntu Gutsy Gibbon. This is the only guide that worked for me and I tried lots of them… was about to give up. THANK YOU for the nice walkthrough!

    Comment by Shawn — March 9, 2008 @ 11:57 pm | Reply

  9. You are a star ! Tried other “how to’s ” and this is the only one that worked first time.

    Thanks for doing the hard work so I didn’t have to 🙂

    Comment by Davros Patel — March 28, 2008 @ 9:22 pm | Reply

  10. Bhai Ji – Thank you for the information!

    I’m a perl/C++ new to Tomcat. I can tell you anything you want to know about perl, DBD::mysql, etc.. so on so forth.

    Now, I need to install Hipergate, so I need to familiarize myself with Tomcat, java, etc. My simple question is this: if I enable mod_jk, can I still use other modules without issue such as mod_perl, php, etc?


    Comment by CaptTofu — April 25, 2008 @ 12:30 pm | Reply

  11. Excelente tutorial amigo, te felicito!!!! 😉

    Comment by Ronny — May 24, 2008 @ 7:14 pm | Reply

  12. Trying to configure this and I have 1 question:

    How do I setup the server so as I can place jsp files in the tomcat_doc_root instead of /jsp-examples/

    i.e. in the parent directory of /jsp-examples/


    Davros P

    Comment by Davros Patel — June 25, 2008 @ 10:20 pm | Reply

  13. Hi

    Thanks for excellent tutorial. However I have come up against a problem I get the following error when restarting apache.

    Syntax error on line 5 of /etc/apache2/mods-enabled/jk.load:
    JkLogStampFormat takes one argument, The Tomcat module log format, follow strftime syntax

    any idea what this means? I am a bit of a newbie to tomcat and command line linux.

    Many Thanks


    Comment by Dafydd — September 6, 2008 @ 7:55 pm | Reply

  14. Hi, I have tried this but to no avail. I have wrote a stand alone chat application. I know my code works perfectly because I have wrote it in uni and it works fine. I want my MIDlet to connect to a servlet I have wrote, In uni we simply save the class in a certain directory. But obivously the directories are different on my laptop as in uni I am working from own uni server space. Does anyone know the directory in which I save my classes so that when my MIDlet calls http://localhost:8180//servlet/ChatServlet15
    It will see my servlet

    Thank you

    Comment by James — October 26, 2008 @ 11:36 am | Reply

  15. Great tutorial.

    I have had much headache while choosing correct java on my linux. I tried to use apt-get, but it could not find most packages, using synaptics just worked and now I have my java and tomcat working on linux. I previously used mac OS X for this. I’d hope a little more explaining on what do all the lines do in this tutorial, but still great.


    Comment by Aki — November 25, 2008 @ 9:10 am | Reply

  16. ok thx

    Comment by doez — April 22, 2009 @ 6:12 am | Reply

  17. Just a quick fix, the port for localhost is
    localhost:8080, and not 8180 ! probably just a typo.
    Goodluck, and thanks for this; your work is appreciated !

    Comment by Ehknaton — March 26, 2010 @ 7:59 pm | Reply

RSS feed for comments on this post. TrackBack URI

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Create a free website or blog at

%d bloggers like this: