File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Servlet not running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Servlet not running" Watch "Servlet not running" New topic
Author

Servlet not running

Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I am new to webdevelopment. I am using Tomcat 5 on Windows 2000. The examples that came with Tomcat works fine. I compiled my own servlet and when I try to run it I get a 404 error. Please help me on how I should configure Tomcat to run by servlet.
Regards,
Maru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
That is a pretty sketchy description, but based on the observed probability of various errors made by new users, I would say that you have run into the "invoker" servlet problem. Take a look at the JavaRanch FAQ.
Bill
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I am very sorry for not providing details to my problem. All the examples that cam with Tomcat work when used http://localhost:8080/servlet-examples/session.
I created my own servlet (a very simple one HelloWorld). I compiled and placed my servlet in a folder called mywebapp. When I use http://localhost:8080/mywebapp/HelloWorld I get 404 error. Since I am new to this area I thought there may be configuration issue I have to do to properly use Tomcat.
Regards,
Maru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Yes there are configuration issues.
If you want Tomcat to recognize TOMCAT_HOME/webapps/mywebapp directory as a web application, then mywebapp directory must have a WEB-INF subdirectory which must have a web.xml file and a classes subdirectory. The web.xml file must define the mapping of your servlet class to the HelloWorld URL.
Furthermore, your servlet class should be in a package.
I consider it unfortunate that the Tomcat 5 distribution chooses to continue using the invoker servlet in the servlet-examples (check the web.xml for invoker) - it just makes things harder when folks try to set up their own web application because it takes the "invoker" shortcut.
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Unfortunate Tomcat 5 factoid #2:

Tomcat 5 example app uses pre-compiled JSPs. So if your JSP configuration is not happy either (say, JAVA_HOME is not properly set), you won't know about that either.

grrrrr.

And with Tomcat 5, they've commented not only the mapping, but the servlet declaration as well.
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
You all are very helpful and I must say thank you. I followed your advice and did some reading. Yes it does work now. However, I am not sure how packaging works in a servlet set-up. Could you please give me a code example if at all possible.
Regards,
Maru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
The package situation is really very simple. In order to avoid really weird and hard to trace bugs, ALL classes used in servlets or jsp should be defined as being in packages. You have to observe the following conventions:
1. when placing the compiled class files under WEB-INF\classes the complete package directory has to be created. So my classes for my phonetic lookup example live in:
webapps\phonetic\WEB-INF\classes\com\wbrogden\phonetic
because the package statement is:
package com.wbrogden.phonetic ;
2. the declaration of servlets in web.xml must name the classes correctly as in this example:

3. when using JavaBeans in JSP, you have to give the complete package when declaring the bean, AND you have to import the package.
Bill
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
Thank you Bill. I am learning as I go. I now understand the reason why using a package is a good idea. Why is it that the servlet-name "metaphone" different from the servlet-class "MetaphoneServ"?
I am lost on the following:
<init-param>
<param-name>listshome</param-name> <param-value>c:/Servers/Data/Phonetic</param-value></init-param>
What does c:/Servers/Data/Phonetic mean in this context? I thought you stored the servlet classes in web\phonetic\WEB-INF\classes\com\wbrogden\phonetic.
Thank you for your help and sorry for taking more of your time.
Regards,
Maru
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
1. My use of metaphone for the servlet name is purely arbitrary.
2. The init-param contents are used to pass configuration data to the servlet. In this case it is the directory on my hard drive where the wordlist files are kept. In the init() method of the servlet I grab that value to initialize the servlet data - like this:

where the locateHashmaps method uses the param-value matching the "listshome" param-name.
Bill
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
Thank you very much for taking the time to help me. I need now to buy a book (hope to get a good book) to learn Servlet and JSP.
Regards,
Maru
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet not running