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

查看:170
本文介绍了无法导入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消失,但仍然尝试构建和清理项目。我不在日食上使用tomcat,而仅在网豆上使用它。我该如何解决这个问题。

不要放入 servlet-api .jar 在您的项目中。这只是自找麻烦。如果动态Web模块方面设置为3.0版,则需要检查项目属性的项目方面部分。您还需要确保声明您的 /WEB-INF/web.xml (如果有的话)符合Servlet 3.0规范。即< web-app> 根声明必须与以下内容匹配:

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这样的完全有价值的servlet容器,然后在项目属性的 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.

  • How do I import the javax.servlet API in my Eclipse project?

与具体问题无关: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天全站免登陆