7

I know that each servlet container (e.g. Tomcat, Glassfish, etc.) comes with an implementation of servlet-api.

What I am looking for is a generic (container agnostic) version of servlet-api to compile my application code against.

I would also like source code or Javadoc to use in my IDE during development.

Does such a version of servlet-api.jar exist?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ChrisH
  • 4,788
  • 26
  • 35

2 Answers2

10

For pure compilation, it doesn't matter which one you picks. You could extract it from Tomcat's /lib directory or grab it from some Maven repo. Please pay attention that you pick the correct version, 2.3, 2.4, 2.5 or 3.0 which is the same as the target runtime and that you never include it in webapp's /WEB-INF/lib.

Since you're using an IDE (based on your question history, I'll bet Eclipse), you could also just associate the dynamic web project with a target runtime. This way Eclipse will automagically include the necessary libs for compiletime.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can I be sure that if I put the servlet-api.jar from Tomcat in my Eclipse project class path that there is no way for me to accidentally write application code that would not work with Glassfish or some other servlet container? – ChrisH May 10 '11 at 15:55
  • As long as the library does not end up in webapp's `/WEB-INF/lib`. You'll otherwise run in trouble when the webapp is deployed on a servletcontainer supporting a newer servlet API version. Just setting it as target runtime (the same as your test servletcontainer) in Eclipse is strongly recommended, I honestly don't see why you would ever workaround/takeover it like that. – BalusC May 10 '11 at 15:57
  • I understand the API version and classpath issues. I'm just really surprised that Sun/Oracle doesn't seem to provide a standard servlet-api.jar that the implementers then implement. Why rely on the implementers to convert a text based specification document into a binary API? – ChrisH May 10 '11 at 16:06
  • 1
    They used to, [here](http://www.oracle.com/technetwork/java/javaee/servlet/index.html), but nowadays you're forced to download a whole Glassfish server around it. I think it's the Oracle's marketing team. I believe there is still some page around where you can "officially" download some old servlet API version. Let me look... – BalusC May 10 '11 at 16:10
  • Ok, I thought it was strange that I couldn't get a Java EE SDK without Galssfish. Thanks for confirming I'm not missing something. – ChrisH May 10 '11 at 16:12
  • Yes, [here](http://www.oracle.com/technetwork/java/download-139443.html), check the "download class files" on 2.2 and 2.3 versions. It's however missing on 2.4 and newer. – BalusC May 10 '11 at 16:17
1

What is important is that you import and compile against the javax.servlet versions of these classes, rather than the vendor specific implementations of these interfaces.

DaveH
  • 7,187
  • 5
  • 32
  • 53