1

While coding a servlet I found a method that says

Since:
    Servlet 3.1

I guess that if I have the autohint from NetBeans to use it is because I have that Servlet version. But I cannot find a place to confirm that. I'm using glassfish4.1 as container. If I go to mypathtoglassfish4.1\glassfish\modules there I can see javax.servlet-api.jar and inside a manifest that says:

Implementation-Version: 3.1.0

Is that the proper way to check that? I'm especially interested in being able to tell my colleagues "go to that jar and check that property" so I'm sure that my code will run on their server.

As alternative, I found a webpage Oracle GlassFish Server 3.1 Application Development Guide that says: "GlassFish Server supports the Java Servlet Specification version 3.0." but obviously for Glassfish 3.1, and I couldn't find one of those for every glassfish version (not even for mine -4.1 )

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
malarres
  • 2,941
  • 1
  • 21
  • 35

2 Answers2

8

Look at Jakarta EE version history. Servlet (and JSP, JSF, EJB, JPA, etc) version goes hand in hand with Jakarta EE version:

  • Jakarta EE 10 = Servlet 6.0
  • Jakarta EE 9 = Servlet 5.0
  • Jakarta/Java EE 8 = Servlet 4.0
  • Java EE 7 = Servlet 3.1
  • Java EE 6 = Servlet 3.0
  • Java EE 5 = Servlet 2.5
  • J2EE 1.4 = Servlet 2.4
  • J2EE 1.3 = Servlet 2.3
  • J2EE 1.2 = Servlet 2.2

And look at GlassFish version history:

  • GlassFish 7.x = Jakarta EE 10
  • GlassFish 6.x = Jakarta EE 9
  • GlassFish 5.x = Jakarta/Java EE 8
  • GlassFish 4.x = Java EE 7
  • GlassFish 3.x = Java EE 6
  • GlassFish 2.x = Java EE 5 + clustering support
  • GlassFish 1.x = Java EE 5

So your GlassFish 4.1 is Java EE 7 / Servlet 3.1.

But, with a big but, that's one thing. The second thing is, the webapp's web.xml version also plays a role. Not everyone knows that.

If your webapp's web.xml is declared conform Servlet 3.1 like below,

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <!-- Config here. -->
</web-app>

then your webapp will also really run in Servlet 3.1 modus.

However, if it's declared conform Servlet 3.0 like below or even older,

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->
</web-app>

then your webapp will run in Servlet 3.0 compatibility modus, even when deployed to a Servlet 3.1 compatible container! The above influences the ServletContext#getMajorVersion() and getMinorVersion(), so they actually say nothing about the container, but only about the webapp itself.

If your webapp's web.xml contains a <!DOCTYPE>, regardless of the DTD and the version, then it will run in Servlet 2.3 compatibility modus, even when there's a newer XSD declared!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun.com/dtd/web-app_2_3.dtd">
<web-app 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <!-- This is WRONG! The DOCTYPE must be removed! -->
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • So does that mean that I can just adjust the version to 3.0 without having to install another jdk? (btw thanks for your imageservlet!) – malarres Jun 11 '15 at 16:19
  • Without having to install an older Java EE application server version matching that servlet version, yes. JDK isn't that relevant, you should just pick the minimum JDK/JRE version the server itself requires. You're welcome :) – BalusC Jun 11 '15 at 19:21
1

First, take a look to Comparing GlassFish Open Source Edition versions 2.x and 3.0.x. Also in your servlet you can add

HttpSession session = request.getSession();
int majorVersion = session.getServletContext().getMajorVersion();
int minorVersion = session.getServletContext().getMinorVersion();
Mihai8
  • 3,113
  • 1
  • 21
  • 31
  • Thanks, I tried a shot from the hip: https://java.net/projects/glassfish/public/comparing_v3_and_v4.html but didn't work :( I'm going to look up better inside this page – malarres Jun 11 '15 at 10:03
  • I created a minimalistic servlet just for showing the info from your code. On my system at least it works :) Will let you know once they check it – malarres Jun 11 '15 at 10:32
  • Wrong approach. See my answer for right approach and explanation. – BalusC Jun 11 '15 at 10:49
  • Now I understand why they got an error 500 when trying my minimalistic servlet. Thanks! – malarres Jun 11 '15 at 16:16