且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法导入javax.servlet.annotation.WebServlet;

更新时间:2023-12-04 14:45:55


我尝试将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.