aspose file tools*
The moose likes Tomcat and the fly likes everything dandy except servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "everything dandy except servlet" Watch "everything dandy except servlet" New topic
Author

everything dandy except servlet

Terri-Lynn Rimmer
Greenhorn

Joined: Dec 02, 2004
Posts: 3
I'm having a servlet problem with Tomcat. My webapp looks like this:

I can access my index.jsp through http://localhost/ but when I try to go to http://localhost/Test it gives me a 404 error saying page can not be found on this server.
index.jsp is located in webapps folder
in that folder is a WEB-INF (in capitals) where web.xml and the code above is located.
in that folder is a classes folder with Test.class in it
Any ideas?


Baffled
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5

It is very likely that you are having the problem because the class is not in a package. If any class used in a servlet or JSP is not in a package, the JVM looks in the "current" directory - since you have no control over the current directory this always fails.
Therefore - ALL classes used in servlet or JSP must be in a package and the location of the compiled class file must match the package.
It used to be possible to get away with this when the "invoker" servlet was on by default because the invoker could locate the class. See this page in the ranch FAQ for more.
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I agree with what William says, but I'd point out that the servlet *itself* does not need to be in a package (in my experience).

I think the problem might be where you're putting things.

You say that "index.jsp is located in webapps folders". Don't you mean in a subfolder of webapps?

So do you have a folder like:

If this is what you have, then you have two applications.

(A) can be accessed as: http://localhost/index.jsp
(B) can be accessed as: http://localhost/test/index.jsp

Therefore the test servlet would be accessed as http://localhost/test/Test

If you just have an index.jsp and WEB-INF folder under webapps directly, then this would not work at all.
Terri-Lynn Rimmer
Greenhorn

Joined: Dec 02, 2004
Posts: 3
Thanks for replying so quickly! I've already tried putting it in a package but I had it working in windows before without it.
Linux:
webapps
|
|-terri
| |
| |-WEB-INF
| |-web.xml
| |-classes
| |
| |-terric
| |
| |-Test.class with package terric;

and changed the web.xml url pattern turned into /terric.Test and also tried it as /terri/terric.Test and changed the servlet name into terric.Test
and I'd try accessing it through http://localhost/terri/terric.Test
And well I knew very well this wouldn't work anyway. None of the servlets are working not even under /servlet-examples. I also tried the invoker and nothing, of course. But, jsp pages are working fine. I guess it would have to do with my httpd.conf file? I have tomcat hooked up to apache but everythings fine aside from the .class files and I've tried to manipulate the web.xml file in every imaginable way.
Terri-Lynn Rimmer
Greenhorn

Joined: Dec 02, 2004
Posts: 3
woops that file structure didnt come out right lets try again:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
I agree with what William says, but I'd point out that the servlet *itself* does not need to be in a package (in my experience).

How in the world do you do that without having the invoker running or writing your own classloader?
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

William.

Very simply. Try it.



The *actual* problem, now that I copy and pasted the Original Poster's web.xml, is that the elements are not correct.

Terri-Lynn: Check out what I've posted above.

(you've got servlet-name and servlet-class elements inside both the servlet and servlet-mapping elements.)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Well, thats a surprise to me - I suppose it is related to the

servlet-mapping steering tomcat to the right directory.
Learn something new every day.
Bill
Vignes Rajagopal Achary
Greenhorn

Joined: Jan 02, 2005
Posts: 1
I had the same problem, You have to make sure the web.xml has the correct title, also that depends on what tomcat version you're running.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


The above is the right title for the web.xml for new servlet and
tomcat, don't try anything from text books that were more than a year old.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat's classloaders searches for classes in the following places, in this order:

  • Bootstrap classes of your JVM
  • System class loader classses (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/i18n/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_BASE/shared/classes
  • $CATALINA_BASE/shared/lib/*.jar


  • That being said, I would strongly advise against using any non-packaged classes in a webapp, servlet or otherwise.


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: everything dandy except servlet