eclipse 在 pom.xml 中为 Tomcat 7 设置 Servlet/JSP jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13982955/
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
Setting up Servlet / JSP jars in pom.xml for Tomcat 7
提问by california6586
I am using JDK 1.6, Maven 2.2.1, Tomcat 7 and Eclipse Juno (Enterprise Edition).
我使用的是 JDK 1.6、Maven 2.2.1、Tomcat 7 和 Eclipse Juno(企业版)。
When trying to import packages such as javax.servlet.*; Eclipse complained that it does not have it inside its classpath (build path).
尝试导入诸如 javax.servlet.* 之类的包时;Eclipse 抱怨它的类路径(构建路径)中没有它。
So, what I did was include the following dependencies in my pom.xml file:
所以,我所做的是在我的 pom.xml 文件中包含以下依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
During tomcat server startup:
在tomcat服务器启动期间:
INFO: Deploying web application archive C:\DevTools\tomcat\apache-tomcat-7.0.32\webapps\myapp.war
Dec 20, 2012 4:55:41 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\DevTools\tomcat\apache-tomcat-7.0.32\webapps\myapp\WEB-INF\lib\servlet-api-2.3.jar)
- jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Why is it logging this message?
为什么会记录此消息?
Also, I can't seem to find the right versions for servlet-api 3.0 or jsp 2.2 in the maven repository
另外,我似乎无法在 maven 存储库中找到 servlet-api 3.0 或 jsp 2.2 的正确版本
Kindly help me...
请帮助我...
回答by Perception
It is the responsibility of the web application container to provide both the Servlet/JSP API and implementation files. With your current Maven setup you are causing these library files to be bundled in your app, which consequently causes the class loader problems you are seeing in your log.
Web 应用程序容器有责任提供 Servlet/JSP API 和实现文件。使用您当前的 Maven 设置,您将这些库文件捆绑在您的应用程序中,从而导致您在日志中看到的类加载器问题。
When trying to import packages such as javax.servlet.*; Eclipse complained that it does not have it inside its classpath (build path).
尝试导入诸如 javax.servlet.* 之类的包时;Eclipse 抱怨它的类路径(构建路径)中没有它。
Yes, you need the API libraries in your classpath in order to compile code that depends on them. You want to include these libraries on your buildclasspath, while at the same time not including them in the final deployable. You do this with Maven by identifying the scope
of the dependent libraries as provided
. Which basically means "these libraries are needed for compilation, but will eventually be provided by the app container, so don't actually include them in the deployable".
是的,您需要类路径中的 API 库才能编译依赖于它们的代码。您希望在构建类路径中包含这些库,同时不将它们包含在最终的可部署中。您可以通过scope
将依赖库的 标识为来使用 Maven 执行此操作provided
。这基本上意味着“编译需要这些库,但最终将由应用程序容器提供,因此实际上不要将它们包含在可部署中”。
Also, I can't seem to find the right versions for servlet-api 3.0 or jsp 2.2 in the maven repository
另外,我似乎无法在 maven 存储库中找到 servlet-api 3.0 或 jsp 2.2 的正确版本
The Servlet 3.0 and JSP 2.2 API's have been bundled in the common JavaEE Web API. This is the dependency that you need to specify in your POM. Illustrated:
Servlet 3.0 和 JSP 2.2 API 已捆绑在通用 JavaEE Web API 中。这是您需要在 POM 中指定的依赖项。插图:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
回答by Raghuram
Tomcat comes with servet and jsp jars. So you would want to exclude them from your war file. This can be done by specifying a providedscope.
Tomcat 带有 servet 和 jsp jar。所以你想从你的War文件中排除它们。这可以通过指定提供的范围来完成。
Add <scope>provided</scope>
to both the dependencies and try it again.
添加<scope>provided</scope>
到两个依赖项并重试。
回答by Himanshu
You shouldn't be adding these JSP and Servlet dependencies to your project. Instantiations of the J2EE specification such as servlets should be provided by the application server's runtime.
您不应该将这些 JSP 和 Servlet 依赖项添加到您的项目中。J2EE 规范的实例化(例如 servlet)应该由应用程序服务器的运行时提供。
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.
在 Eclipse 中,为您的应用程序服务器添加服务器运行时。右键单击项目并选择属性。然后构建路径 > 添加库 > 服务器运行时。