Why is the servlet API jar not included with Web facet?

Whenever I want to create a simple Servlet project, I have to manually add the servlet-api.jar file to the classpath. Why is this necessary? I can right-click on the src directory and the pop-up menu includes an option to create a new servlet. However, when I do, I always get a "package javax.servlet.http does not exist" error until I manually add the servlet API dependency. It seems to me that if I indicate that the module requires a web application facet, the servlet API should be included as a dependency automatically. Am I missing something?

4 comments
Comment actions Permalink

Hi Dustin,

That makes sense. I created corresponding ticket about that - IDEA-55457, feel free to vote/monitor it.

There is a following workaround at the moment - you can get semi-automated library addition at the following way:

  1. Open your servlet class at IDEA editor;
  2. Put caret to any unresolved class from servlet API and perform 'quick fix' action:

servlet-quickfix1.png
3. Select 'Add library 'Servlets' to classpath' fix - IDEA will does the necessary stuff:
servlet-quickfix2.png

Regards, Denis

1
Comment actions Permalink

Thanks Denis! I didn't realize that a quick fix would resolve this problem. Your suggestion works great. Though it would be better if IDEA included these dependencies automatically when a web application project is created, the quick fix approach sure beats having to add the Java EE jars manually.

0
Comment actions Permalink

The J2EE implementation jar is implementation specific. How would IntelliJ know which one you want to use? It would depend on which container you setup as a dependency. Albeit since it is a standard you are usually ok using any implmentation in your IDE and even at compile time. However, seems safer to use the specific implmenation that will be used at runtime.

If you do have some app servers setup in IntelliJ though it probably would be cool for IntelliJ to ask you which one you want to use if you do include a Servlet or another relevent class.

0
Comment actions Permalink

Sure. IntelliJ could prompt you for the app server to which you are deploying and use those JEE jars. That would be cool. However, as you stated, any implementation should work at compile time as those jars will ultimately be provided by the container. I just find it surprising to create a new web module and then not have the classpath configured for a web application. Fortunately, a simple quick fix call solves the problem but I think that including a default implementation of these JARs would be more intuitive.

0

Please sign in to leave a comment.