views:

520

answers:

3

I'm trying to learn how to develop a servlet to run under Apache's Tomcat server on Windows XP. I'm using Tomcat 6.0 with Java SDK 1.6.0_17. My webapp is a simple Hello, world type which I'm building using the latest version of the Eclipse IDE. I'm able to run all the examples in the Tomcat installation but when I export my app from Eclipse to the Tomcat webapps folder and try to access it from my browser, I get errors pointing to the fact that the javax.servlet.http.Servlet class couldn't be found. I searched the SDK tree and couldn't find any thing with javax in the name so I'm guessing it isn't there.

I've googled around but haven't found anything with this exact, same issue. Any suggestions what I can try?

+1  A: 

the Servlet class in servlet.jar, which is part of JEE (the "Enterprise" part of Java) and not the regular JDK distribution. It's usually in Tomcat's classpath so once you get your app deployed to Tomcat there's little trouble finding it.

Usually, the problem with this jar is that it's not normally in Eclipse's classpath, so you end up with compile-time problems.

If it's really missing at runtime on Tomcat, try digging for it in your project's library dependencies (I can never figure out where Eclipse hides that stuff) and copy it into the "common/lib" subdirectory of your Tomcat installation.

Carl Smotricz
+1  A: 

It should be available in servlet-api.jar in Tomcat's own classpath, i.e. the Tomcat/lib folder.

If you get this particular error, there's probably more at matter. You maybe duplicated one or more of Tomcat's own libraries (one of the files in Tomcat/lib) into Eclipse or, more worse, the JDK/lib. You shouldn't do that. Keep all default libraries there where they belongs. Never duplicate/move them around. Only reference them.

If you want to develop webapps using Eclipse, you should have got at least the Eclipse for Java EE Developers and integrated Tomcat in Eclipse through the Servers view and have created a Dynamic Web Project wherein you have selected the afore-integrated Tomcat instance as the default server instance.

For more hints about using Eclipse and Tomcat together, you may find this tutorial useful (you may fully skip and ignore the JSF part if that is not of interest).

Edit: I realized that javax.servlet.http.Servlet class actually doesn't exist. You need either javax.servlet.Servlet or javax.servlet.http.HttpServlet. But that would already have failed during compiletime, not during runtime, so your problem was maybe not described very accurate.

BalusC
One correction to that: The servlet-api.jar is servlet.jar's little brother: It satisfies the dependencies enough to avoid compile errors, so it's usually in Eclipse's build path; but for the actual functionality, one needs servlet.jar to be available where the webapp is running.
Carl Smotricz
The filename is appserver specific. Tomcat doesn't have a servlet.jar, but calls it servlet-api.jar.
BalusC
addendum: and Eclipse can only find it if you integrated the server in Eclipse and associated it with the project. You should NOT manually copy/reference it in Eclipse build path or so.
BalusC
A: 

Thank you all for your replies. It would appear that something is broken in my setup. I've got what I thought was an identical configuration on another computer that is working. Now I just need to figure out what exactly is different between the two environments.

Larry