Running Servlets in Apache Tomcat

13,917

Solution 1

You need to let the servletcontainer know that you've a servlet which it has to execute. Since you're already on Tomcat 7.0, a @WebServlet annotation should suffice.

@WebServlet(urlPatterns={"/servlet/BeeServlet"})
public class BeeServlet extends HttpServlet {
    // ...
}

Or the old way (probably as the JavaRanch tutorial should have mentioned), by a declaration in web.xml.

<servlet>
    <servlet-name>beeServlet</servlet-name>
    <servlet-class>BeeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>beeServlet</servlet-name>
    <url-pattern>/servlet/BeeServlet</url-pattern>
</servlet-mapping>

Please note that putting classes in a default package is a bad practice. You should be placing classes in a package if you want them to be visible to classes inside a package. The servletcontainer, written in pure Java, needs to be able to see them as well. Now, Tomcat has hacks for this, but this works in specific versions/configurations only. Always, always put servlet classes in a package.

See also:


Unrelated to the concrete problem:

I've installed Java EE

Please note that the Java EE download from Oracle.com contains basically the Glassfish application server along with some documentation. You don't need it when all you want is just running servlets on Tomcat.

See also:

Solution 2

Apache is a web server, not a servlet/JSP engine. Tomcat is a servlet/JSP engine; so is Jetty. You'll need to deploy your servlets/JSPs on Tomcat and tell Apache to forward requests to your servlets/JSPs to Tomcat.

Share:
13,917
dvanaria
Author by

dvanaria

I'm currently working for a telecommunications company in Colorado, doing some software development and systems integration work. I've been interested in programming from an early age, from about when I was 13 or so, programming on Apple II systems at school and eventually at home when my father bought me an Apple IIc. I loved picking up programming books from the public library and just picking out whatever interested me and trying it out first hand. I went on to learn C programming in college, then OpenGL graphics programming, Object Oriented Programming with Java, just about anything new to me I found interesting. Today I still work on my own programming projects (now usually in Python and C++), but I've always had great memories of how much fun it was to discover programming when I was a kid. I'm more interested these days in getting other people interested in computers and programming, maybe trying to inspire other people (especially kids) to get into it. I'm always going back to the basics and really trying to break concepts down so they are accessible and so I understand them better myself. My hope is to one day either write a programming book for kids, that recaptures some of that wonder and excitement, or develop a series of YouTube tutorials that may help newbies pick up programming in a way that is more accessible and easily understood. I think today's biggest barrier to entry in this field of interest is the complexity of today’s systems. It's not like the old days where you turned your computer on and it booted in a few seconds into a BASIC command prompt. Those systems were a lot of fun because you had to pick up programming right from the start in order to really do anything with them. Either way, this site (Stack Overflow) has been a tremendous help to me and a lot of fun to contribute to. Ideally, I would love to feel some kind of expertise with programming in general, and this site is a good step toward getting that kind of experience – the best way to learn anything, I’m convinced, is to teach others, to ask a lot of questions, and help out other people by answering their questions.

Updated on June 04, 2022

Comments

  • dvanaria
    dvanaria almost 2 years

    I'm working through the Java Ranch Cattle Drive online tutorials and got up to the Servlets projects. I wanted to install and run Apache instead of Orion, because I wanted to learn a more mainstream HTTP server.

    I got Apache up and running on my machine (this is a Windows XP/Cygwin environment, so I'm using the Apache package that comes with the latest version of cygwin, currently httpd version 1.3.33)

    I'm to the point of directing a browser to http://localhost and the server is correctly fetching the welcome page (index.html) at C:\cygwin\var\www\htdocs.

    I've installed Java EE and was able to compile the following Servlet:

    import java.io.* ;
    import javax.servlet.http.* ;
    
    public class BeeServlet extends HttpServlet
    {
    
        public void doGet( HttpServletRequest request , HttpServletResponse response )
        {
            response.setContentType("text/html");
            try
            {
                PrintWriter out = response.getWriter();
                out.println( "a-buzz-buzz ..." );
                out.close();
            }
            catch( Exception e )
            {
                System.out.println( "cannot get writer: " + e );
            }
        }
    
    }
    

    This compiles into a .class file without errors. My question is, where do I install this file in the server?

    The file is called BeeServlet.class and the tutorial says to direct a browser to http://localhost/servlet/BeeServlet after installing the BeeServlet.class in the appropriate directory in the web server.

    (EDIT: I've successfully installed Tomcat and have the basic welcome page showing, as explained in the steps below. I'm still not sure where to put the .class file though or how to access it):

    Here's are the steps of installing Tomcat and running it through Cygwin:

    1. Go to http://tomcat.apache.org/ and download the latest version of Tomcat (for the above system configuration, select the 32-bit/64-bit Windows Service Installer method, which will create a 9 MB installation at C:\Program Files\Apache Software Foundation\Tomcat 7.0).

    2. Add this path to the Windows system environment variable 'Path'

    3. Start a Cygwin bash shell

    4. type 'tomcat7' (with Path set, it will find this .exe in the above path). This will start the tomcat server.

    5. Start a browser and direct it to http://localhost:8080. This will bring up the Tomcat welcome screen (which is really Tomcat reading the file: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\index.jsp).

    6. Create new directories under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps named examples\WEB-INF\classes.

    7. Add a @WebServlet annotation to the source code file (this would be located after any imports): @WebServlet(urlPatterns={"/servlet/BeeServlet"}). Compile the BeeServlet.java file and place the .class file in C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\examples\WEB-INF\classes\BeeServlet.class

    8. Direct your browser to http://localhost:8080/examples/servlet/BeeServlet