Thursday, October 18, 2007

Running Servlets on Fedora 7


I was wondering for last two days to compile and run my java servlet programs on Fedora. But I couldn't find any useful information on internet. After much research and trial and errors I finally got to run servlets on Fedora. And this is how I did this, I think this might be useful for another novice like me. I have used the backward approach for this tutorial. It means problems first and then the solutions. Of course I could have used the straight approach like do this this and this and you are done. But I think that wont be much useful as you wouldn't know what thing is needed for what.

1. First thing first, before running servlets you need to compile them. And for that you need Sun Java SDK installed on your system. I'm assuming that you have it already on your system. If not you can go through this blog to find how to install it in Fedora.

2. For compiling and running you also need tomcat installed on your system. You can choose Tomcat alone or with Apache. I preferred the second one. Install Apache by the command

yum install httpd

and install Tomcat on Fedora by the command

yum install tomcat5 tomcat5-webapps tomcat5-admin-webapps

here tomcat5 is the latest version for fedora.

3. Here is one sample program that I copied and pasted from a tutorial site. We'll use this sample program for our part of tutorial.

import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DemoServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();
out.println("<title>Hello World!</title>");
out.println("<h1>Hello World!</h1>");

Type the program in a text editor and save the file as

So like every java program, we'll use javac to compile the program.


Even if you didn't did any typing mistakes, you are more likely to get the errors package javax.servlet does not exist
import javax.servlet.*;
^ package javax.servlet.http does not exist
import javax.servlet.http.*;

If this is the case it means that our CLASSPATH environment is not set. Workaround for this problem is to
append the line
export CLASSPATH=/usr/share/java/servletapi5.jar
to your .bash_profile or .bashrc file. These files will be in your home directory and will be hidden.
Or one can alternatively type the above command each time he logins to compile servlet programs.

After adding the line you have to do a logout and login to reset the environment variables. Now again compile the program. Now it should not give the above errors and if you have typed the program correctly it'll compile successfully.

4. So we have compiled the program now how to run it. We can't just type java DemoSevlet to run the program here. Now we need Tomcat. login as root and make a directory in the tomcat web-application directory by the command

mkdir /var/lib/tomcat5/webapps/test

In this directory make another directory WEB-INF

mkdir /var/lib/tomcat5/webapps/test/WEB-INF

Inside this directory make directory classes

mkdir /var/lib/tomcat5/webapps/test/WEB-INF/classes

Inside classes directory we'll keep our servlet source and corresponding compiled class files. So copy the and DemoServlet.class files to this directory.

cp /path/to/ /var/lib/tomcat5/webapps/test/WEB-INF/classes/
cp /path/to/DemoServlet.class /var/lib/tomcat5/webapps/test/WEB-INF/classes/

Replace /path/to with your own paths to and DemoServlet.class
Now in WEB-INF directory we'll create web.xml file which will give information about our servlets to Tomcat server.

While you are still logged as root go to the directory

cd /var/lib/tomcat5/webapps/test/WEB-INF/

Create the file web.xml using gedit

gedit web.xml

Now you should enter the following contents in the file and then save and quit.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""

In the file the tags <servlet-name> and <servlet-class> gives the information about servlet-name and servlet-class and the tag <url-pattern> tells about the URL pattern to run the servlet.

5. Start the Apache server by command

service httpd start

Now test Apache Server by typing address http://localhost/ in your browser.

Start the Tomcat server

service tomcat5 start

Test Tomcat server by typing address http://localhost:8080/ in your browser

Run your servlet by typing address http://localhost:8080/test/servlet/DemoServlet in your browser.


nandu said...

good link for novice user

GENIUS said...

thanks ...
it was of great help...
thanks once again....

scribbleworld said...

that was some interesting piece of information.. I did manage to get servlets running...

Yogesh Nachnani said...

thanks a lot, really helped me out :-)

Ankit said...

hey thanks a lot....was a very useful information

Vajindar Laddad said...

Thanks a million...

geethasan said...

Thanks a lot for each of the baby steps.

Unknown said...

Thanks a lot buddy...

Anonymous said...

Exactly what I needed to understand where to start in practical terms, and what has to be learnt :)
Thank you!

Karthika Shree said...

Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
Java Training in Chennai