Maven tomcat7-maven-plugin不能与@WebServlet一起使用

时间:2016-05-19 07:32:24

标签: maven servlet-3.0 tomcat7-maven-plugin

我在servlet3.0中使用Servlet创建@WebSevlet,如下所示:

@WebServlet(urlPatterns = {"/demo2"}, loadOnStartup = 1)
public class Demo2Servlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("[Demo2Servlet][work]");
    }
}

我在maven中添加插件tomcat7-maven-plugin,如下所示:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <port>8080</port>
        <path>/web30</path>
    </configuration>
</plugin>

当我使用命令tomcat7:run时,它会给我一个错误:

Fatal: Servlet /web30 threw load() exception
java.lang.ClassCastException: demo2.sevlet.Demo2Servlet cannot be cast to javax.servlet.Servlet
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
.......

但如果我在本地使用tomcat7,它运作良好,为什么?

我不明白为什么错误是ClassCastException,我扩展了HttpServlet !!

=============================================== ======================== 感谢@Vikdor,你是对的,在servlet-api依赖中添加<scope>provided</scope>,它很棒。

0 个答案:

没有答案