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

查看:1194
本文介绍了无法导入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在我的eclipse只有它在我的网豆。如何解决问题。

不要将 servlet-api.jar 。这只是问麻烦。如果动态Web模块 facet设置为版本3.0,则需要检查项目属性的 Project Facets 部分。您还需要确保您的 /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不可执行在不同的make /版本的生产容器上。您将获得所有颜色的类路径相关的错误/异常。

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天全站免登陆