如何找到 glassfish 服务器的 servlet API 版本?

How to find servlet API version for glassfish server?

在编写 servlet 代码时,我发现了一个方法

Since:
    Servlet 3.1

我想如果我有来自 NetBeans 的自动提示来使用它是因为我有那个 Servlet 版本。但是我找不到可以确认的地方。我使用 glassfish4.1 作为容器。如果我去 mypathtoglassfish4.1\glassfish\modules 那里,我可以看到 javax.servlet-api.jar 并且在清单中显示:

Implementation-Version: 3.1.0

这是检查它的正确方法吗?我特别感兴趣的是能够告诉我的同事 "go to that jar and check that property" 所以我确信我的代码会 运行 在他们的服务器上。

作为替代方案,我找到了一个网页 Oracle GlassFish Server 3.1 Application Development Guide,上面写着:"GlassFish Server supports the Java Servlet Specification version 3.0." 但显然对于 Glassfish 3.1,我找不到每个 glassfish 版本的其中一个(甚至对于我的 - 4.1 )

首先,看一下Comparing GlassFish Open Source Edition versions 2.x and 3.0.x。同样在您的 servlet 中,您可以添加

HttpSession session = request.getSession();
int majorVersion = session.getServletContext().getMajorVersion();
int minorVersion = session.getServletContext().getMinorVersion();

看看Java EE version。 Servlet(和 JSP、JSF、EJB、JPA 等)版本与 Java EE 版本齐头并进。

  • 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 = 小服务程序 2.4
  • J2EE 1.3 = 小服务程序 2.3
  • J2EE 1.2 = 小服务程序 2.2

查看服务器 homepage/documentation 它是如何呈现的。对于 GlassFish,即当前(4.1):

World's first Java EE 7 Application Server

所以,这是 Servlet 3.1。

但是,but 是一回事。第二件事是,webapp 的 web.xml 版本也起作用。不是每个人都知道这一点。

如果您的网络应用 web.xml 被声明为符合如下所示的 Servlet 3.1,

<?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>

那么您的 webapp 也将真正 运行 在 Servlet 3.1 模式中。

但是,如果它被声明为符合 Servlet 3.0 如下或更早版本,

<?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>

那么您的 Web 应用程序将 运行 采用 Servlet 3.0 兼容方式,即使部署到 Servlet 3.1 兼容容器中也是如此!以上影响了 ServletContext#getMajorVersion()getMinorVersion(),所以他们实际上没有提到容器,只提到了 webapp 本身。

如果您的网络应用程序的 web.xml 包含 <!DOCTYPE>,无论 DTD 和版本如何,那么它将 运行 在 Servlet 2.3 兼容性模式中,即使有更新的 XSD宣布!

<?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>