9

I want use SDK Java EE 7, Glassfish 4 and Maven.
Is it correct? Please, draw attention to scopes.

1. For servlets:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

2. JSP without standart tags and without JSTL:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.0</version>
</dependency>

3. For JSP with standard tags "c:"

<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

4. For JSP with JSTL

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>

Do you know specification where this information contains?

Aleks Ya
  • 859
  • 5
  • 15
  • 27
  • 2
    You're misunderstanding the meaning of "standard" taglibs. It does not mean that it contains only c tags. It's basically the whole JSTL 1.1.2 implementation from Apache. Then, you've another JSTL 1.2 implementation which is the reference implementation (usually, the one from Sun/Oracle). They will obviously only conflict with each other. Basically, you end up with two different JSTL implementations. You should be declaring only one of them. Do note that Glassfish already ships with it out of the box, so the scope should obviously be set to "provided". – BalusC Jun 26 '13 at 15:24

2 Answers2

14

There are a variety of options. As suggested in the question, one approach is to import the whole Java EE API. But you can also be more selective. You can instead just include the servlet API (this is for servlet API 3.0.1; more recent versions are available for the same artifact data, but older versions use the artifact id servlet-api instead):

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

The question implies that the JSTL package also pulls in the relevant JSP dependencies; this isn't the case: if you need to use the JSP API at all, you do need a dependency for it (but it's worth noting that you don't necessarily need it, as discussed at this question). You should use the correct version of the JSP API that matches the Servlet API version you're using, so for servlet API 3.0.1 as I show above, you should use 2.2:

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>

Like for the Servlet API, there have been changes in the dependency data for the JSP API; in this case for versions older than 2.0 the group id is just javax.servlet, while for versions newer than 2.2 the artifact id has changed to javax.servlet.jsp-api.

For JSTL, you should almost certainly just be using version 1.2. The new standard location for this version is:

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

although the old location as shown in the question continues to work correctly. Presumably, should any future updates be made to this library they will be included with this group/artifact ID, as this appears to be designed to fit appropriately with all the other most recent artifacts. Unlike the other artifacts, JSTL is not provided by the container, so the scope should not be set to "provided" as for the others.

Community
  • 1
  • 1
Jules
  • 14,841
  • 9
  • 83
  • 130
6

You shouldn't be adding these dependencies to your project. Instantiations of the J2EE specification such as servlets should be provided by the application server's runtime.

In Eclipse, To add the Server Runtime for your application server. Right click the project and select Properties. Then Build Path > Add Library > Server Runtime.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Expect of JSTL? Why exactly? Glassfish as being a full fledged Java EE container already ships with JSTL out the box. Aren't you confusing with Tomcat which is merely a barebones JSP/Servlet container which indeed doesn't bundle all the other Java EE artifacts? – BalusC Jun 26 '13 at 15:23
  • @BalusC Your exactly right! I was thinking of Tomcat when I said to exclude JSTL from that list. Aside from the JSTL error I was merely trying to point out that you should rely on your server for these libraries, especially given that you might create version conflicts. – Kevin Bowersox Jun 26 '13 at 15:38
  • 7
    This answer is totally incorrect. Maven builds web apps without reference to the server runtime, so does definitely need information about which APIs are available, whichever server you're running. – Jules Apr 25 '17 at 02:09
  • I build my apps with Maven and ```package``` them to ```war``` for the Tomcat Server to run. What should I be doing in this case then? – Varun Gawande Jun 05 '21 at 18:35