3

我正在使用 JDK 1.6、Maven 2.2.1、Tomcat 7 和 Eclipse Juno(企业版)。

尝试导入 javax.servlet.* 等包时;Eclipse 抱怨它的类路径(构建路径)中没有它。

所以,我所做的是在我的 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>

在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

为什么要记录此消息?

此外,我似乎无法在 maven 存储库中找到 servlet-api 3.0 或 jsp 2.2 的正确版本

http://mvnrepository.com/

请帮助我...

4

3 回答 3

8

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.

When trying to import packages such as javax.servlet.*; Eclipse complained that it does not have it inside its classpath (build path).

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 build classpath, 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".

Also, I can't seem to find the right versions for servlet-api 3.0 or jsp 2.2 in the maven repository

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:

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
于 2012-12-21T02:47:32.620 回答
2

Tomcat 带有servet 和jsp jars。因此,您希望将它们从您的战争文件中排除。这可以通过指定提供的范围来完成。

添加<scope>provided</scope>到两个依赖项并重试。

于 2012-12-21T01:50:55.077 回答
0

您不应该将这些 JSP 和 Servlet 依赖项添加到您的项目中。J2EE 规范的实例化(例如 servlet)应该由应用程序服务器的运行时提供。

在 Eclipse 中,为您的应用程序服务器添加服务器运行时。右键单击项目并选择属性。然后构建路径 > 添加库 > 服务器运行时。

于 2017-01-08T12:34:56.083 回答