java 了解谁提供servlet-api.jar,是web-container还是Java EE下载的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25256768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:41:12  来源:igfitidea点击:

Understanding who provides servlet-api.jar, is it web-container or part of Java EE download

javajakarta-eeservletstomcat7

提问by CuriousMind

I need understanding about the serlvet-api.jar which is needed to compile a servlet.

我需要了解编译 servlet 所需的 serlvet-api.jar。

I am building a simple servlet, like this:

我正在构建一个简单的 servlet,如下所示:

import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {
    // Remaining code here
}

I know that we need servlet-api.jar file to compile this simple servlet, like this:

我知道我们需要 servlet-api.jar 文件来编译这个简单的 servlet,如下所示:

javac -classpath /path/where/jar/is/servlet-api.jar

Now my doubts starts here:

现在我的疑惑从这里开始:

  1. What is servlet-api.jar?
  2. Who provides this jar?
  3. Does each web-container provide this jar e.g., Tomcat, Jboss, glassfish? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet.
  4. When we download Java EE , is this jar part of download? OR do we get this file as part of web container?
  5. Consider this situation:

    Suppose we compile / build the simple servlet using Tomcat (i.e tomcat's version of jar needed to build the servlet) and create a .war file. Can we then deploy the war in some other vendor container?

  1. 什么是servlet-api.jar?
  2. 谁提供这个罐子?
  3. 每个 web 容器都提供这个 jar,例如 Tomcat、Jboss、glassfish 吗?并且每个供应商是否为构建这个简单的 Servlet 所需的 jar 提供了“相同的名称”。
  4. 当我们下载 Java EE 时,这个 jar 是下载的一部分吗?或者我们是否将此文件作为 Web 容器的一部分?
  5. 考虑这种情况:

    假设我们使用 Tomcat 编译/构建简单的 servlet(即构建 servlet 所需的 tomcat 版本的 jar)并创建一个 .war 文件。然后我们可以在其他供应商容器中部署War吗?

采纳答案by icza

What is it?

它是什么?

The servlet-api jar is a library which contains the interfaces and classes of the Servlet API specification. The servlet-api jar contains only the interface (the API) of the Servlet Specification, so you can use it to develop your web application.

servlet-api jar 是一个包含 Servlet API 规范的接口和类的库。servlet-api jar 仅包含 Servlet 规范的接口(API),因此您可以使用它来开发您的 Web 应用程序。

Where can you get it?

你在哪里可以得到它?

It is provided at the link below:

它在以下链接中提供:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Where it is contained/bundled

它包含/捆绑的地方

Servlet-api.jar is part of the Java EE download so you can develop your web applications (you could not compile your FirstServletclass if the Java EE would not contain it).

Servlet-api.jar 是 Java EE 下载的一部分,因此您可以开发您的 Web 应用程序(FirstServlet如果 Java EE 不包含它,您将无法编译您的类)。

Servlet containers (like Tomcat, JBoss, GlassFish etc.) also contain the servlet-api.jar else they would not be able to run your web application, and moreover they also contain the implementation of the interfaces that are part of the Servlet API.

Servlet 容器(如 Tomcat、JBoss、GlassFish 等)也包含 servlet-api.jar,否则它们将无法运行您的 Web 应用程序,而且它们还包含作为 Servlet API 一部分的接口的实现。

The name is not always the same though, and it might not even exist as a separate jar, the Servlet API classes might be bundled in another jar.

虽然名称并不总是相同,它甚至可能不作为单独的 jar 存在,Servlet API 类可能捆绑在另一个 jar 中。

You can however download a separate jar file containing only the Servlet API if you just want to develop a web application for a Servlet container, or if you want to create/write your own Servlet API implementation. Look at here:

但是,如果您只想为 Servlet 容器开发 Web 应用程序,或者您想创建/编写自己的 Servlet API 实现,则可以下载仅包含 Servlet API 的单独 jar 文件。看这里:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Portability

可移植性

You can compile your web application if you have the Servlet API, no matter where it comes from. After you compiled your web app, you can optionally pack it into a WAR file (WAR=Web ARchive) which is simply a zip file containing your static files, your compiled java classes and configuration files like web.xmletc. And you will be able to run your compiled web application in any Servlet containers (but read forward).

如果您拥有 Servlet API,则可以编译您的 Web 应用程序,无论它来自何处。编译 Web 应用程序后,您可以选择将其打包到一个 WAR 文件(WAR=Web ARchive)中,该文件只是一个包含静态文件、编译的 Java 类和配置文件web.xml等的 zip 文件。您将能够运行您在任何 Servlet 容器中编译的 Web 应用程序(但请向前阅读)。

So answer to your question #5 is:

所以你的问题#5 的答案是:

There are multiple versions of the Servlet API, and there are more to the Java EE platform than just the Servlet API (e.g. Enterprise Java Beans). But it's safe to say that if you only use the Servlet API, all Servlet containers that implement that version of the Servlet API will be able to run your web application.

Servlet API 有多个版本,Java EE 平台不仅限于 Servlet API(例如 Enterprise Java Beans)。但是可以肯定地说,如果您只使用 Servlet API,那么所有实现该版本 Servlet API 的 Servlet 容器都将能够运行您的 Web 应用程序。

The configuration files of the different web applications might differ though (which is outside of the Servlet API scope), so you should always check the documentation of the target web application.

尽管不同 Web 应用程序的配置文件可能有所不同(这在 Servlet API 范围之外),因此您应该始终检查目标 Web 应用程序的文档。

回答by Luiggi Mendoza

What is servlet-api.jar?

什么是servlet-api.jar?

It is a jar that provides the necessary interfaces/classes to write Servlets.

它是一个提供编写 Servlet 所需的接口/类的 jar。

Who provides this jar?

谁提供这个罐子?

Any servlet container such as Jetty or Tomcat and any Java EE compliant application server like JBoss/Wildfly, GlassFish, IBM WebSphere, Oracle WebLogic, etc.

任何 servlet 容器,如 Jetty 或 Tomcat,以及任何符合 Java EE 的应用服务器,如 JBoss/Wildfly、GlassFish、IBM WebSphere、Oracle WebLogic 等。

Does each web-container provide this jar e.g., Tomcat, Jboss, glassfish? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet.

每个 web 容器都提供这个 jar,例如 Tomcat、Jboss、glassfish 吗?并且每个供应商是否为构建这个简单的 Servlet 所需的 jar 提供了“相同的名称”。

Yes for the servlet api because it is a set of interfaces, which enables programming to interfaces rather than class implementationsso we avoid programming to a specific application server. The name of the jar containing the implementation must not have part of this name in common.

servlet api 是的,因为它是一组接口,它允许对接口而不是类实现进行编程,因此我们避免对特定的应用程序服务器进行编程。包含实现的 jar 的名称不得与该名称的一部分相同。

When we download Java EE , is this jar part of download? OR do we get this file as part of web container?

当我们下载 Java EE 时,这个 jar 是下载的一部分吗?或者我们是否将此文件作为 Web 容器的一部分?

Java EE download available in Oracle is just GlassFish. This is covered previously.

Oracle 中提供的 Java EE 下载只是 GlassFish。这在前面已经介绍过了。

Suppose we compile / build the simple servlet using Tomcat (i.e tomcat's version of jar needed to build the servlet) and create a .war file. Can we then deploy the war in some other vendor container?

假设我们使用 Tomcat 编译/构建简单的 servlet(即构建 servlet 所需的 tomcat 版本的 jar)并创建一个 .war 文件。然后我们可以在其他供应商容器中部署War吗?

As long as you don't use a Tomcat specific class, library or feature, then yes, there would be no problem. Otherwise, no.

只要您不使用 Tomcat 特定的类、库或功能,那么是的,就不会有问题。否则,没有。

回答by jcool

It is the server container vendor which needs to provide this servlet-api you can find it in this directory tree C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib (it depends on your container download location). And of course it is provided by other containers.

需要提供这个servlet-api的是服务器容器供应商,你可以在这个目录树C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib中找到它(这取决于你的容器下载位置)。当然,它是由其他容器提供的。

回答by yuranos

A lot of things can easily be checked based on POM files. For example, Tomcat Catalinahas a reference to tomcat-servlet-api(newer version of org.apache.tomcat:servlet-api)

很多事情可以很容易地基于 POM 文件进行检查。例如,Tomcat Catalina引用了 tomcat-servlet-api(较新版本的org.apache.tomcat:servlet-api

Notice also, and it can cause some confusion when investigating dependencies trees, that Tomcat Embeddoesn't use any servlet-api jar at all and instead just specifies Servlet API contract inside itself.

另请注意,在调查依赖关系树时可能会引起一些混淆,Tomcat Embed根本不使用任何 servlet-api jar,而是仅在其内部指定 Servlet API 协定。