Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Servlet / Web app on Tomcat on Linux tutorial

  1. #1
    Join Date
    Dec 2004
    Location
    Cameron Park, CA
    Beans
    22
    Distro
    Ubuntu 6.10 Edgy

    Servlet / Web app on Tomcat on Linux tutorial

    I'm sure this has been done before, but why not one more? In my initial forays into writing, compiling, deploying, running servlets on Tomcat running on Linux, I encountered extreme frustration. Frankly, the documentation/tutorials out there, including the ones at the Tomcat website, the ones at the Sun website, and others, are lacking. They tell you how to write a servlet. They tell you to create a WAR. They tell you to deploy the WAR to the CATALINA_HOME directory, etc etc.

    However, the tutorials often severely lack a lot of the necessary details, and I would always run into stumbling blocks. So I would Google this, Google that, paruse message boards, and fight with it until I would finally come up with a solution.

    Sure, I was always able to easily do servlets with Tomcat using NetBeans, or some other great tool. But I always ran into problems trying to do it all manually in the commnand line (a worthwhile endevour, to understand how everything works and is put together properly, rather than relying on the IDE crutch).

    With all this, you will have to install Tomcat from the debian repos, as well as Java itself. Using apt-get to install Tomcat should give everything you need, but you might also want to instal the regular Sun Java SDK, or install Eclipse (to bring it's dependencies), or install the free Java SDK from the Deb repos (which includes the Jikes compiler, the SableVM virtual machine, and GNU classpath). In any case, having a full Java install, and Tomcat, might be the subject of another tutorial. But this tutorial is centered on getting a web app / servlet compiled, deployed and running on Tomcat on Kanotix. So it assumes you already installed Java and Tomcat.

    So, here is how it all works (at least for me, with a Debian install and Tomcat5, and Java 1.5 (as well as gcj):

    1. Create Web Application directory:
    NameOfProjectDirectory
    NameOfProjectDirectory/WEB-INF
    NameOfProjectDirectory/WEB-INF/classes (optional sub directories like sample here)
    NameOfProjectDirectory/WEB-INF/lib (optional, if needed)

    2. In NameOfProjectDirectory/WEB-INF/classes (/subdir) Compile:
    javac -cp /usr/share/tomcat5/common/lib/servlet-api.jar NameOfServlet.java

    the -cp argument is, of course, providing the classpath to the servlet jar file. Without providing this, the servlet won't compile. You could, of course, set the CLASSPATH environment variable as well.

    3. In NameOfProjectDirectory (root) Make WAR file:
    jar -cvf NameOfWarFile.war .
    (don't forget the ".", with a space between it and the war - it's important)

    4. Stop Tomcat (if already running)
    su
    /etc/init.d/tomcat5 stop

    Here, you log on as root, ("su"), and it will prompt you for root's password. Root access is necessary to stop/start Tomcat, and is necessary to copy the WAR into the webapps directory of Tomcat.

    5. copy WAR file to webapps directory of Tomcat:
    su
    cp NameOfWarFile.war /usr/share/tomcat5/webapps

    Here, cp is the the Linux copy command, not to be confused with the -cp argument to javac.

    6. Start Tomcat:
    su
    /etc/init.d/tomcat5 start

    7. In browser:
    http://localhost:8180/NameOfWAR/NameOfServlet (or how it's named in URL pattern tag of web.xml)

    In my Tomcat install on my Debian/Kanotix install, Tomcat is configured to port 8180. With most Tomcat installs, it defaults to 8080. I'm not sure what is is for the Ubuntu install (I did all this on my Kanotix box, not my Ubuntu box), but I assume it's the same.

    Servlet sample code (HelloServlet.java):

    package test;

    import java.io.*;

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

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

    out.println("<h1>Hello, world!</h1><br>");
    out.println("<h1>Jeff loves Servlets!</h1>");
    out.close();
    }
    }

    web.xml Sample code:

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
    xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
    <servlet>
    <servlet-name>hello</servlet-name>
    <!-- in this example, the servelt is in a subdir/package called test -->
    <servlet-class>test.HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <!-- accessed in browser, or HTML/JSP link, via "/NameOfWar/hello" -->
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    </web-app>

    If one wants to add JSPs or html forms (that call the servlet), it's a matter of having them in the root of your project directory (above WEB-INF). If there are additional classes that the servlet(s) depends on or accesses, put them in NameOfProjectDirectory/WEB-INF/lib

    Also, there is an easier way of deploying the servlet (leaving the command line a bit). It's in the Tomcat Manager page (accessed, of course, in your browser). First, you have to add a user account in the tomcat-users.xml file (the xml there is pretty self explanatory, you just have to make it an administrator). Then you have to bring up the Tomcat homepage in your browser - http://localhost:8180/ - then you click on the Tomcat Manager Link on the left, and you'll be prompted to enter your user id and password. After than, in the Manager page, scroll down to the deploy war file option, and click on the browse button to browse to where you created the WAR. Then click on "Deploy". Then it's ready to go. No loggin on as root in the command line, not stopping Tomcat, no copying the WAR to the webapps of Tomcat directory, not restarting Tomcat.

    Then, to be even easier, one can create an Ant script that does everything automatically. But that's the subject of another tutorial.

    Finally, it's brain-dead easy to create web apps in NetBeans. But it's very very very worthwhile to do it all in the command line, in order to learn how everything works and is put together.

    I hope this helps for any Tomcat/Servlet newbies out there!
    "What if there were no hypothetical questions?" - George Carlin

    "Beer is living proof that God loves us and wants us to be happy." - Benjamin Franklin

  2. #2
    Join Date
    Nov 2004
    Beans
    2,614

    Re: Servlet / Web app on Tomcat on Linux tutorial

    You don't, in a default install of Tomcat5, need to stop or stop the server for it to notice a deployed WAR.

    You don't even need to in a non-default install, you just have to command it manually to stop and start the webapplication.

    Also, in developer mode (The default), you can deploy an already exploded WAR tree to webapps and use that. It's perfectly legal and allows you to change .JSPs on the fly without having to remove and redeploy the WAR. They are automatically recompiled on every change.

    However, certain things like changes to configuration files will not be caught unless you change web.xml, which will cause a complete redeploy. I have had issues with this in the past, and tend to manually redeploy (or kickstart the server) when making such changes. But depending on the development you're doing, this may be eaiser than doing other things.

  3. #3
    Join Date
    Dec 2004
    Location
    Cameron Park, CA
    Beans
    22
    Distro
    Ubuntu 6.10 Edgy

    Re: Servlet / Web app on Tomcat on Linux tutorial

    Quote Originally Posted by LordHunter317
    You don't, in a default install of Tomcat5, need to stop or stop the server for it to notice a deployed WAR.

    You don't even need to in a non-default install, you just have to command it manually to stop and start the webapplication.

    Also, in developer mode (The default), you can deploy an already exploded WAR tree to webapps and use that. It's perfectly legal and allows you to change .JSPs on the fly without having to remove and redeploy the WAR. They are automatically recompiled on every change.

    However, certain things like changes to configuration files will not be caught unless you change web.xml, which will cause a complete redeploy. I have had issues with this in the past, and tend to manually redeploy (or kickstart the server) when making such changes. But depending on the development you're doing, this may be eaiser than doing other things.
    Those are some subtleties (and conveniences) that I have yet to discover in Tomcat. I'm glad you added to that. Thanks.
    "What if there were no hypothetical questions?" - George Carlin

    "Beer is living proof that God loves us and wants us to be happy." - Benjamin Franklin

  4. #4
    Join Date
    Jul 2005
    Location
    UK
    Beans
    573

    Re: Servlet / Web app on Tomcat on Linux tutorial

    For those looking to do Tomcat development, try Eclipse with the plugin by Sysdeo.

    Howto:
    http://www-128.ibm.com/developerworks/library/os-ectom/

    Plugin:
    http://www.sysdeo.com/eclipse/tomcatplugin

  5. #5
    Join Date
    Dec 2008
    Location
    Outer Shpongolia
    Beans
    Hidden!

    Re: Servlet / Web app on Tomcat on Linux tutorial

    can anyone tell me how to add the servlet-api.jar to the classpath so i dont have to manually add it to the classpath each time. thanks
    What arrests my attention...
    is the fact, that this space is inhabited.

  6. #6
    Join Date
    Dec 2008
    Location
    Outer Shpongolia
    Beans
    Hidden!

    Re: Servlet / Web app on Tomcat on Linux tutorial

    I figured it out its export CLASSPATH=$CLASSPATH:/directory/to/your/class

    add it to /ect/bash.bashrc to keep it persistent
    Last edited by Shpongle; October 9th, 2010 at 07:53 PM.
    What arrests my attention...
    is the fact, that this space is inhabited.

  7. #7
    Join Date
    Feb 2010
    Beans
    8

    Re: Servlet / Web app on Tomcat on Linux tutorial

    Okay, I am in the same boat being a newbie to servlet development. I have Ubuntu 10.10 all setup with tomcat6 running. I have installed the "openjdk". When I try and compile a Java program is get the following errors, even with the classpath set.

    m@ubuntu:~/myapp$ echo $CLASSPATH
    /usr/share/tomcat6/lib/servlet-api.jar
    m@ubuntu:~/myapp$

    m@ubuntu:/var/lib/tomcat6/webapps/k2$ !386
    sudo javac HelloWorld.java
    HelloWorld.java:4: package java.servlet does not exist
    import java.servlet.ServletException;
    ^
    HelloWorld.java:5: package java.servlet.http does not exist
    import java.servlet.http.HttpServlet;
    ^
    HelloWorld.java:6: package java.servlet.http does not exist
    import java.servlet.http.HttpServletRequest;
    ^
    HelloWorld.java:7: package java.servlet.http does not exist
    import java.servlet.http.HttpServletResponse;
    ^
    HelloWorld.java:9: cannot find symbol
    symbol: class HttpServlet
    public class HelloWorld extends HttpServlet {
    ^
    HelloWorld.java:10: cannot find symbol
    symbol : class HttpServletRequest
    location: class HelloWorld
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    ^
    HelloWorld.java:10: cannot find symbol
    symbol : class HttpServletResponse
    location: class HelloWorld
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    ^
    HelloWorld.java:11: cannot find symbol
    symbol : class ServletException
    location: class HelloWorld
    throws ServletException, IOException {
    ^
    8 errors




    THX for the help ahead of time.
    Last edited by midnight1111; December 5th, 2010 at 04:48 AM.

  8. #8
    Join Date
    Apr 2008
    Beans
    507

    Re: Servlet / Web app on Tomcat on Linux tutorial

    Those servlet classes are in the javax.servlet package not java.servlet.
    Go you good thing!

  9. #9
    Join Date
    Feb 2010
    Beans
    8

    Re: Servlet / Web app on Tomcat on Linux tutorial

    m@ubuntu:/var/lib/tomcat6/webapps/devguide/WEB-INF/classes$ sudo javac HelloWorld.java
    HelloWorld.java:6: class HelloServlet is public, should be declared in a file named HelloServlet.java
    public class HelloServlet extends HttpServlet {
    ^
    HelloWorld.java:3: package javax.servlet.http does not exist
    import javax.servlet.http.*;
    ^
    HelloWorld.java:4: package javax.servlet does not exist
    import javax.servlet.*;
    ^
    HelloWorld.java:6: cannot find symbol
    symbol: class HttpServlet
    public class HelloServlet extends HttpServlet {
    ^
    HelloWorld.java:7: cannot find symbol
    symbol : class HttpServletRequest
    location: class HelloServlet
    public void doGet (HttpServletRequest req,
    ^
    HelloWorld.java:8: cannot find symbol
    symbol : class HttpServletResponse
    location: class HelloServlet
    HttpServletResponse res)
    ^
    HelloWorld.java:9: cannot find symbol
    symbol : class ServletException
    location: class HelloServlet
    throws ServletException, IOException
    ^
    7 errors
    m@ubuntu:/var/lib/tomcat6/webapps/devguide/WEB-INF/classes$ echo $CLASSPATH
    /usr/share/tomcat6/lib/servlet-api.jar
    m@ubuntu:/var/lib/tomcat6/webapps/devguide/WEB-INF/classes$ echo $JAVA_HOME
    /usr/lib/jvm/java-1.6.0-openjdk
    m@ubuntu:/var/lib/tomcat6/webapps/devguide/WEB-INF/classes$

  10. #10
    Join Date
    Feb 2010
    Beans
    8

    Re: Servlet / Web app on Tomcat on Linux tutorial

    Still getting the same error as listed above.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •