Freely Scripted Soul

November 18, 2007

Using Servlets in Fedora Core 7

Filed under: Software and Technology — Roshan @ 4:07 pm
Tags: , , , ,

All commands and descriptions are CASE SENSITIVE

 

 

STEP 1: INSTALLATION

 

 

First of all, install Java6, Apache, Tomcat5.5 and all related packages by using your installation DVD(while installing Fedora itself or after installation, boot from the DVD and “upgrade” your system by selecting the required packages).

NOTE: Tomcat is an optional package under “Web Servers”. So click on “Web Servers” and then on “Option Packages”. Browse the list and “tick” all Tomcat related packages.

 

 

STEP 2: SETTING UP

 

 

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

$ gedit $HOME/.bashrc

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

export JAVA_HOME=/usr/share/java

export CATALINA_HOME=/etc/tomcat5

export CLASSPATH=.:/var/lib/tomcat5/common/lib/[servlet].jar

PATH=$PATH:$JAVA_HOME:$CATALINA_HOME:$CLASSPATH:.

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 copy it to the folder “/etc/tomcat5”:

$ cp web.xml /etc/tomcat5/

Setup is done.

 

 

STEP 3: RESTART

 

 

Go to System->Administration->Server Settings->Services

The “Services” window will open. On the left-side pane, one-by-one, “tick” the boxes and click on the “Start” button for the following services: httpd, mysqld, tomcat5

You should get appropriate messages that the services have been started successfully.

Click on the “Save” button to save these settings permanently.

Restart the system for the changes to take effect.

 

 

STEP 4: TESTING

 

 

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

http://localhost:8080

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:

http://localhost:8080/servlets-examples/

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 HelloWorld.java

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

Compile this file:

$ javac HelloWorld.java

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

 

 

STEP 5: USING

 

 

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

/var/lib/tomcat5/webapps/servlets-examples/WEB-INF/classes

For example:

$ cd /var/lib/tomcat5/webapps/servlets-examples/WEB-INF/classes

$ gedit HelloWorld.java

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

$ javac HelloWorld.java

Before running it, you need to TWEAK the “web.xml” file. Run this command at the terminal:

$ gedit /var/lib/tomcat5/webapps/servlets-examples/WEB-INF/web.xml

Click on the “Replace” button or press “Ctrl+H” from the keyboard.

You need to replace all instances of “HelloWorldExample” with your filename i.e. “HelloWorld”.

So enter “HelloWorldExample” in “Search For” box and “HelloWorld” in “Replace with” box and click “Replace All”.

Save and close the file.

IMPORTANT: This TWEAKING is necessary for ONLY the first time you are executing a Servlet in your system. Once you tweak this file and execute your Servlet successfully, just FORGET about this file. (I don’t quite understand the logic behind this. It quite illogical! However, servlets work only if you tweak web.xml in this way! Maybe a bug in Fedora7 😉 )

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

http://localhost:8080/servlets-examples/servlet/HelloWorld

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 “servlets-examples” folder of Tomcat.

For example:

$ cd /var/lib/tomcat5/webapps/servlets-examples

$ gedit HelloWorld.html

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

Open this link in your browser:

http://localhost:8080/servlets-examples/HelloWorld.html

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

 

DOWNLOAD LINK:

Web.xml – http://www.esnips.com/doc/ca71aa2c-ec61-4fbb-8b0e-85a670ef6c2f/web-fedora7

 

 

 

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 Uday S Athreya for the wonderful research 🙂

 

Advertisements

11 Comments »

  1. Bose these are all the errors i got:-(
    wat to do!

    ———-
    1. ERROR in HelloWorld.java (at line 2)
    import javax.servlet.*;
    ^^^^^^^^^^^^^
    The import javax.servlet cannot be resolved
    ———-
    2. ERROR in HelloWorld.java (at line 3)
    import javax.servlet.http.*;
    ^^^^^^^^^^^^^
    The import javax.servlet cannot be resolved
    ———-
    3. ERROR in HelloWorld.java (at line 5)
    public class HelloWorld extends HttpServlet{
    ^^^^^^^^^^^
    HttpServlet cannot be resolved to a type
    ———-
    4. ERROR in HelloWorld.java (at line 6)
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    ^^^^^^^^^^^^^^^^^^
    HttpServletRequest cannot be resolved to a type
    ———-
    5. ERROR in HelloWorld.java (at line 6)
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    ^^^^^^^^^^^^^^^^^^^
    HttpServletResponse cannot be resolved to a type
    ———-
    6. ERROR in HelloWorld.java (at line 7)
    throws ServletException,IOException{
    ^^^^^^^^^^^^^^^^
    ServletException cannot be resolved to a type

    Comment by Ajay — November 19, 2007 @ 4:47 am | Reply

  2. You get such errors if the “web.xml” isnt modified.

    Comment by Roshan — November 24, 2007 @ 8:39 am | Reply

  3. @Ajay

    you got those errs because u have not exported CLASSPATH
    i.e CLASSPATH should point to servlet.jar which contains all the classes(Httprequest..Httpresponse..etc) required for servlet execution.

    Comment by pavan p — November 25, 2007 @ 8:15 am | Reply

  4. First of all, very long time since i visited ur blog
    Super webLog -> it rocks.

    @Roshan >>>In reply to[”
    >Before running it, you need to TWEAK the
    > “web.xml” file. Run this command at the
    > ……..
    >elloWorld” in “Replace with” box and
    >click “Replace All”.
    “]

    Well I think you are referring to servlet mapping here, thats what it is called.
    No, its not a bug. Its a standard introduced by sun so that all java servers map to servlets in a proper way. Its not an headache but is quite handy sometimes. Anyway as far as my knowledge goes, this is how it is done =>

    MyFile
    MyPackage.MyFile

    MyFile
    /mypath/myfile

    There are mainly 2 tags the servlet and the servlet-mapping tag, servlet tags name is mapped to its class inside the tag, in the above eg `MyFile is your class inside a package called `MyPackage.
    If you dont have your own package its ok, just write `MyFile (i.e., if MyFile is name of your class in MyFile.java and is compiled to .class)
    Servlet Mapping is used for mapping the servlet name to the url as the tag suggests so if your
    writing the url pattern in the tag as
    `/mypath/myfile
    then u execute in browser as
    `localhost/mypath/myfile
    It is important to note that everything is case sensitive and / should precede the text inside the url pattern.
    Now what you were doing in your tweak was replacing their HelloWorldExample servlet with your HelloWorld servlet. Instead of doing this, you can use the above steps to create your own servlets without disturbing the existing ones.

    If your question is “Why is this new mapping thingy important”, if its such a pain.
    Ans=>Firstly it helps us by giving different path names to the visitor without revealing the actual path of our classes(abstraction as well as security). Secondly other security things such as j_security_check and other login types can be mentioned in such mappings. This is very helpful while making a proper jsp servlet powered website. yes, u can also seprate the business logic servkets and user view servlets and also the controller.
    ______________________________________
    More about servlet mapping
    http://e-docs.bea.com/wls/docs90/webapp/configureservlet.html

    Comment by Nik — November 25, 2007 @ 8:35 pm | Reply

  5. oh no, the idiotic security thing stripped off the left and right angled braces . aNyway I will try to put it once agin below, if it doesnt come, refer to the web page

    <servlet>
    <servlet-name>MyFile</servlet-name>
    <servlet-class>MyPackage.MyFile</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>MyFile</servlet-name>
    <url-pattern>/mypath/myfile</url-pattern>
    </servlet-mapping>

    Comment by Nik — November 25, 2007 @ 8:38 pm | Reply

  6. @pavan
    ya.. u r right.. its CLASSPATH prob.. not web.xml..

    @nik
    u r SIMPLY GREAT!!!!
    sooo much of research!!!
    ya.. i knew its servlet-mapping thing.. but tats for the web.xml file i have asked to download and put in “/etc/tomcat5/”!! this web.xml in “/var/lib/tomcat5/webapps/servlets-examples/WEB-INF/” is different.. there we change the default example name to another dummy thing only for the first time and it works!!!!! if u had to do it everytime, then alteast it wud be logical 😉 but no.. just one time.. to override d default example.. illogical.. so oly said mite be bug antha!!

    Comment by Roshan — November 27, 2007 @ 1:45 am | Reply

  7. @roshan
    SIMPLY GREAT >> u | !me
    no research so pasted such a stupid reply
    I thought u had mistook servlet mapping as a bug.
    But i was foolish in thinking that u cud make a mistake.
    ANyway that was how i used to do it in windows, dont know much about tomcat on fedora(may be dont know anything on fedora).

    Comment by nik — November 27, 2007 @ 5:36 pm | Reply

  8. @nik
    ley.. chill!!!
    dont embarass me.. WITHOUT knowing bout fedora oly u can fix bugs!![;)]

    Comment by Roshan — November 28, 2007 @ 1:39 am | Reply

  9. http://www.google.co.in/search?hl=en&q=tomcat+5+fedora+7&btnG=Google+Search&meta=
    Your search comes on the first page of google when you search ” fedora 7 and tomcat 5 “.
    🙂

    Comment by nik — November 29, 2007 @ 8:03 pm | Reply

  10. super 🙂

    Comment by Roshan — December 5, 2007 @ 2:33 am | Reply

  11. hello friend if i modify any data or add any print line to my java file after compiling its is not at all updating details in the browser. imy web.xml file is correct and create xml file

    “Any idea from ur side”

    Comment by rajesh — November 21, 2008 @ 6:41 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:

WordPress.com Logo

You are commenting using your WordPress.com 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

Create a free website or blog at WordPress.com.

%d bloggers like this: