aspose file tools*
The moose likes Tomcat and the fly likes Running an Applet in Tomcat 4.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Running an Applet in Tomcat 4.0" Watch "Running an Applet in Tomcat 4.0" New topic
Author

Running an Applet in Tomcat 4.0

Suzanne Reiner
Greenhorn

Joined: May 22, 2001
Posts: 22
I have an applet calling a servlet to retreive database info. The servlet uses another class (we'll call it 'homebase') retreive the data. Pretty simple, right?
Problem is, Tomcat is not recognizing 'homebase.' The applet populates with the servlet, however, there is not any data (if I run homebase from the shell, the data is returned). There are not any errors being thrown or logged. I believe the problem to be the issue of the servlet not finding 'homebase' (I had been having trouble compiling the servlet because homebase was not being found in the existing classpathes. After adding the root directory of myApplication to the classpath it compiled.
However, as I understand it, Tomcat does not use the classpath environment variable so... how do I get Tomcat to recognize 'homebase?'
I have the following environment and directory structure:
RedHat7.1, Tomcat 4.0, jdk1.3.1_01
tomcat/webapps/myApplication
The applet and all support files (including the servlet) are in the root directory of 'myApplication.'
Any thoughts?
Suzanne
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

have you set up a servlet mapping?

Search this forum, or the Servlets forum for "servlet mapping" for more info.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16142
    
  21

If "homebase" is a utility class and not something more complex (like another servlet), by all rights you should have been getting a "500" error on your browser and seen a "ClassNotFoundException" in the log -- unless you're trapping and losing the exception somewhere.
The proper place to put utility classes used by servlets and JSPs is usually WEB-INF/classes for loose classfiles and WEB-INF/lib for jarfiles. The application's WEB-INF/classes and each of the jars in WEB-INF/lib are automatically added to the application's CLASSPATH when the app starts up. Don't forget that even under Windows, capitalization of class files has to be exactly the same as specified in the source code, and that package levels require the appropriate subdirectory structure just like in stand-alone Java.


Customer surveys are for companies who didn't pay proper attention to begin with.
Suzanne Reiner
Greenhorn

Joined: May 22, 2001
Posts: 22
I had, previously, created a servlet mapping and had removed it. I'll try it again. However, doesn't the servlet mapping of the web.xml file provide details for servlets inside of WEB-INF only? If so, the applet/servlet combo outside of WEB-INF would not be able to use the servlet mapping... right? Then again... the web.xml is read at server startup...so, all preset properties should be available... yeah?
I have to admit. I'm a bit confused about how/ if/where Tomcat uses the java environment. For example... it seems as if every Tomcat application has 2 environments... the files inside of WEB-INF (and therefore controlled by the servlet api) and the files at yourApplications root directory (aka: 'static' files).
I'm a newbie (obviously so), its fun to learn.. but goodness gracious! so frustrating at times
Thanks,
Suzanne
Suzanne Reiner
Greenhorn

Joined: May 22, 2001
Posts: 22
Originally posted by Tim Holloway:
The proper place to put utility classes used by servlets and JSPs is usually WEB-INF/classes for loose classfiles and WEB-INF/lib for jarfiles. The application's WEB-INF/classes and each of the jars in WEB-INF/lib are automatically added to the application's CLASSPATH when the app starts up.

Mr. Holloway,
When compiling classes for use in tomcat should they be compiled as 'usual'..(using javac)? Or should they be compiled within Tomcat? If so, using what convention? Or will adding "/TomcatRoot/webapps/.:" to my classpath to ensure the java compiler looks there for utility classes?
The reason I ask, is I have completed the changes as you explained them (in addition to readding the servlet mappings to my web.xml file) but, still get the error (this code works fine when using Sun's Java Server).
Thank you for your help,
Suzanne
Suzanne
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I just noticed this: you said: "the applet/servlet combo outside of WEB-INF"

Why is it outside WEB-INF? I admit that I'm not too knowledgable about applets. I've never used them extensively, and not at all to talk to servlets. But my educated guess is to structure things like this:

application root
- HTML files
- applet files
- WEB-INF
- - - web.xml
- - - classes
- - - - - - servlet files in their package directories
- - - - - - 'helper' class files
- - - lib
- - - - - - any jar files (could contain helper classes)

I'd put the applet class files outside of WEB-INF because they are loaded by the HTML's <applet> or <object> tag, and so should be in the . directory. Unless of course, you specify something else in the codebase attribute.

Then from within your applet, you'd contact the servlet through an URLConnection? This would be indistinguishable from a browser making the request (ie: a normal HTTP request from a web surfer), so Tomcat should simply use the mappings and resolve to the appropriate servlet under WEB-INF/classes.

As for compiling, you can just use javac. There are no tomcat-specific utility classes that need to be included when compiling servlets.
Suzanne Reiner
Greenhorn

Joined: May 22, 2001
Posts: 22
Originally posted by Mike Curwen:
Why is it outside WEB-INF?

I ran across a comment (in the Tomcat users archives) by Craig McClanahan:
From the perspective of the web application, the .class and .jar
files for
your applet are just static content, which are downloaded by the
browser
when needed (just like image files are). Therefore, these classes
must be
in some location *other* than /WEB-INF to be visible to the browser.
It is not possible to share .class and .jar files between the webapp
itself and your applet -- you will have to duplicate the necessary
files.

In tying to locate an answer, I misunderstood what was meant. Craig was discussing utility classes shared between an applet and a servlet. Not the same function I was using them in.
Thank you for taking the time to help me out.
Suzanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running an Applet in Tomcat 4.0