无法导入 javax.servlet.annotation.WebServlet [英] Can't import javax.servlet.annotation.WebServlet

查看:54
本文介绍了无法导入 javax.servlet.annotation.WebServlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始编写可以在 Google App Engine 上运行的应用.
但是当我想将我的代码从 Netbeans 使用到 Eclipse 时,我遇到了一个错误:

I have started to write app that can run on Google App Engine.
But when I wanted to use my code from Netbeans to Eclipse I had an errors on:

import javax.servlet.annotation.WebServlet;

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})

错误是:

The import javax.servlet.annotation cannot be resolved
WebServlet cannot be resolved to a type

我尝试将 servlet-api.jar 导入 Eclipse 但还是一样,还尝试构建和清理项目.我没有在我的 Eclipse 上使用 Tomcat,我的 Netbeans 上只有它.我该如何解决问题?

I tried to import the servlet-api.jar to Eclipse but still the same, also tried to build and clean the project. I don't use Tomcat on my Eclipse only have it on my Netbeans. How can I solve the problem?

推荐答案

我尝试将 servlet-api.jar 导入到 Eclipse,但仍然尝试构建和清理项目.我不在我的 Eclipse 上使用 tomcat,只在我的 net-beans 上使用它.我该如何解决这个问题.

不要不要servlet-api.jar 放入您的项目中.这只是自找麻烦.如果 Dynamic Web Module facet 设置为版本 3.0,您需要检查项目属性的 Project Facets 部分.您还需要确保您的 /WEB-INF/web.xml(如果有)被声明为符合 Servlet 3.0 规范.IE. 根声明必须匹配以下内容:

Do not put the servlet-api.jar in your project. This is only asking for trouble. You need to check in the Project Facets section of your project's properties if the Dynamic Web Module facet is set to version 3.0. You also need to ensure that your /WEB-INF/web.xml (if any) is been declared conform Servlet 3.0 spec. I.e. the <web-app> root declaration must match the following:

<web-app
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

为了能够导入javax.servlet的东西,你需要在Eclipse中集成一个像Tomcat这样的完整servletcontainer,然后在项目的Targeted Runtimes中引用它特性.您可以对 Google App Engine 执行相同的操作.

In order to be able to import javax.servlet stuff, you need to integrate a fullworthy servletcontainer like Tomcat in Eclipse and then reference it in Targeted Runtimes of the project's properties. You can do the same for Google App Engine.

再一次,不要像其他人建议的那样将特定于容器的库复制到 webapp 项目中.它会使您的 webapp 在不同品牌/版本的生产容器上无法执行.您将收到所有颜色的与类路径相关的错误/异常.

Once again, do not copy container-specific libraries into webapp project as others suggest. It would make your webapp unexecutabele on production containers of a different make/version. You'll get classpath-related errors/exceptions in all colors.

与具体问题无关:GAE支持 Servlet 3.0.其底层 Jetty 7.x 容器仅支持最大 Servlet 2.5.

Unrelated to the concrete question: GAE does not support Servlet 3.0. Its underlying Jetty 7.x container supports max Servlet 2.5 only.

这篇关于无法导入 javax.servlet.annotation.WebServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆