简体   繁体   English

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

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

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? 什么是servlet-api.jar?
  2. Who provides this jar? 谁提供这个罐子?
  3. Does each web-container provide this jar eg, Tomcat, Jboss, glassfish? 每个web容器都提供这个jar,例如Tomcat,Jboss,glassfish吗? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet. 并且每个供应商是否为构建这个简单Servlet所需的jar提供了“相同的名称”。
  4. When we download Java EE , is this jar part of download? 当我们下载Java EE时,这个jar是下载的一部分吗? OR do we get this file as part of web container? 或者我们是否将此文件作为Web容器的一部分?
  5. Consider this situation: 考虑这种情况:

    Suppose we compile / build the simple servlet using Tomcat (ie tomcat's version of jar needed to build the servlet) and create a .war file. 假设我们使用Tomcat编译/构建简单的servlet(即构建servlet所需的tomcat版本的jar)并创建.war文件。 Can we then deploy the war in some other vendor container? 我们可以在其他供应商的容器中部署战争吗?

What is it? 它是什么?

The servlet-api jar is a library which contains the interfaces and classes of the Servlet API specification. servlet-api jar是一个包含Servlet API规范的接口和类的库。 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),因此您可以使用它来开发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 FirstServlet class if the Java EE would not contain it). Servlet-api.jar是Java EE下载的一部分,因此您可以开发Web应用程序(如果Java EE不包含它,则无法编译FirstServlet类)。

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

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. 如果您拥有Servlet API,则可以编译Web应用程序,无论它来自何处。 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.xml etc. And you will be able to run your compiled web application in any Servlet containers (but read forward). 编译完Web应用程序后,您可以选择将其打包到WAR文件(WAR = Web ARchive)中,该文件只是一个包含静态文件的zip文件,编译的java类和web.xml等配置文件。您将成为能够在任何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 (eg Enterprise Java Beans). Servlet API有多个版本,Java EE平台还有更多版本,而不仅仅是Servlet API(例如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,那么实现该版本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应用程序的文档。

What is servlet-api.jar? 什么是servlet-api.jar?

It is a jar that provides the necessary interfaces/classes to write Servlets. 它是一个jar,它提供了编写Servlet所需的接口/类。

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 eg, Tomcat, Jboss, glassfish? 每个web容器都提供这个jar,例如Tomcat,Jboss,glassfish吗? And does each vendor provide the "same name" to the jar that is needed to build this simple Servlet. 并且每个供应商是否为构建这个简单Servlet所需的jar提供了“相同的名称”。

Yes for the servlet api because it is a set of interfaces, which enables programming to interfaces rather than class implementations so we avoid programming to a specific application server. 对于servlet api是的,因为它是一组接口,它允许编程到接口而不是类实现,因此我们避免编程到特定的应用程序服务器。 The name of the jar containing the implementation must not have part of this name in common. 包含实现的jar的名称不得具有此名称的一部分。

When we download Java EE , is this jar part of download? 当我们下载Java EE时,这个jar是下载的一部分吗? OR do we get this file as part of web container? 或者我们是否将此文件作为Web容器的一部分?

Java EE download available in Oracle is just GlassFish. Oracle中提供的Java EE下载只是GlassFish。 This is covered previously. 这在以前有所涉及。

Suppose we compile / build the simple servlet using Tomcat (ie tomcat's version of jar needed to build the servlet) and create a .war file. 假设我们使用Tomcat编译/构建简单的servlet(即构建servlet所需的tomcat版本的jar)并创建.war文件。 Can we then deploy the war in some other vendor container? 我们可以在其他供应商的容器中部署战争吗?

As long as you don't use a Tomcat specific class, library or feature, then yes, there would be no problem. 只要您不使用Tomcat特定的类,库或功能,那么是的,就没有问题了。 Otherwise, no. 否则,没有。

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). 它是服务器容器供应商,需要提供此servlet-api,您可以在此目录树C:\\ Program Files \\ Apache Software Foundation \\ Tomcat 6.0 \\ lib中找到它(它取决于您的容器下载位置)。 And of course it is provided by other containers. 当然,它是由其他容器提供的。

A lot of things can easily be checked based on POM files. 可以根据POM文件轻松检查很多内容。 For example, Tomcat Catalina has a reference to tomcat-servlet-api(newer version of org.apache.tomcat:servlet-api ) 例如, Tomcat Catalina引用了tomcat-servlet-api(较新版本的org.apache.tomcat:servlet-api

Notice also, and it can cause some confusion when investigating dependencies trees, that Tomcat Embed doesn't use any servlet-api jar at all and instead just specifies Servlet API contract inside itself. 另请注意,在调查依赖树时可能会引起一些混淆, Tomcat Embed根本不使用任何servlet-api jar,而只是在其自身内部指定Servlet API契约。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM