如何在我的 Eclipse 项目中导入 javax.servlet API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4076601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:20:16  来源:igfitidea点击:

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

javaeclipseservlets

提问by tom

I want to develop with Servlets in Eclipse, but it says that the package javax.servletcannot be resolved. How can I add javax.servletpackage to my Eclipse project?

我想在Eclipse中用Servlets开发,但是说javax.servlet无法解析包。如何将javax.servlet包添加到我的 Eclipse 项目?

采纳答案by BalusC

Ensure you've the right Eclipse and Server

确保您拥有正确的 Eclipse 和服务器

Ensure that you're using at least Eclipse IDE for EnterpriseJava developers(with the Enterprise). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without Enterprise), and manually installed some related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Enterprise Java one.

确保您至少EnterpriseJava 开发人员使用Eclipse IDE(使用Enterprise)。它包含用于创建动态 Web 项目和轻松集成 servlet 容器的开发工具(这些工具是 Web 工具平台 WTP 的一部分)。如果您已经有 Java 的 Eclipse IDE(没有 Enterprise),并且手动安装了一些相关的插件,那么很可能没有正确完成。您最好将其丢弃并获取用于 Enterprise Java 的真正 Eclipse IDE。

You also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, Oracle GlassFish, JBoss AS/WildFly, etc. Usually, just downloading the ZIP file and extracting it is sufficient. In case of Tomcat, do notdownload the EXE format, that's only for Windows based production environments. See also a.o. Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use.

您还需要确保您的机器上已经安装了一个 servletcontainer,它至少实现了与生产环境中的 servletcontainer 相同的 Servlet API 版本,例如Apache TomcatOracle GlassFishJBoss AS/ WildFly等。通常,只需下载 ZIP 文件并解压就足够了。如果是 Tomcat,请不要下载 EXE 格式,这仅适用于基于 Windows 的生产环境。另请参见 ao localhost 上的 Tomcat 服务器所需的几个端口(8005、8080、8009)已在使用中

A servletcontainer is a concrete implementation of the Servlet API. Note that the Java EE SDKdownload at Oracle.com basically contains GlassFish. So if you happen to already have downloaded Java EE SDK, then you basically already have GlassFish. Also note that for example GlassFish and JBoss AS/WildFly are morethan just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness. See also a.o. What exactly is Java EE?

servletcontainer 是 Servlet API 的具体实现。请注意,Oracle.com上的Java EE SDK下载基本上包含 GlassFish。因此,如果您碰巧已经下载了 Java EE SDK,那么您基本上已经拥有 GlassFish。另外请注意,例如GlassFish和JBoss的AS / WildFly是不仅仅是一个servletcontainer,他们也支持JSF,EJB,JPA和所有其他Java EE的空想。另请参见 ao Java EE 到底是什么?



Integrate Server in Eclipse and associate it with Project

在 Eclipse 中集成 Server 并将其与 Project 关联

Once having installed both Eclipse for Enterprise Java and a servletcontainer on your machine, do the following steps in Eclipse:

在您的机器上安装了 Eclipse for Enterprise Java 和 servletcontainer 后,在 Eclipse 中执行以下步骤:

  1. Integrate servletcontainer in Eclipse

    a. Via Servers view

    • Open the Serversview in the bottom box.
    • Rightclick there and choose New > Server.
    • Pick the appropriate servletcontainer make and version and walk through the wizard.

      enter image description here

    b. Or, via Eclipse preferences

    • Open Window > Preferences > Server > Runtime Environments.
    • You can Add, Editand Removeservers here.

      enter image description here

  2. Associate server with project

    a. In new project

    • Open the Project Navigator/Exploreron the left hand side.
    • Rightclick there and choose New > Projectand then in menu Web > Dynamic Web Project.
    • In the wizard, set the Target Runtimeto the integrated server.

      enter image description here

    b. Or, in existing project

    • Rightclick project and choose Properties.
    • In Targeted Runtimessection, select the integrated server.

      enter image description here

    Either way, Eclipse will then automatically take the servletcontainer's libraries in the build path. This way you'll be able to import and use the Servlet API.

  1. 在 Eclipse 中集成 servletcontainer

    一种。通过服务器视图

    • 打开底部框中的服务器视图。
    • 右键单击那里并选择New > Server
    • 选择适当的 servletcontainer 制造商和版本并完成向导。

      在此处输入图片说明

    或者,通过 Eclipse 首选项

    • 打开Window > Preferences > Server > Runtime Environments
    • 您可以在此处添加编辑删除服务器。

      在此处输入图片说明

  2. 将服务器与项目关联

    一种。在新项目中

    • 打开左侧的Project Navigator/Explorer
    • 右键单击那里并选择New > Project,然后在菜单Web > Dynamic Web Project 中
    • 在向导中,将目标运行时设置为集成服务器。

      在此处输入图片说明

    或者,在现有项目中

    • 右键单击项目并选择Properties
    • Targeted Runtimes部分,选择集成服务器。

      在此处输入图片说明

    无论哪种方式,Eclipse 都会在构建路径中自动采用 servletcontainer 的库。这样您就可以导入和使用 Servlet API。



Never carry around loose server-specific JAR files

永远不要随身携带松散的特定于服务器的 JAR 文件

You should in any case not have the need to fiddle around in the Build Pathproperty of the project. You should above all nevermanually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.

在任何情况下,您都不需要在项目的Build Path属性中摆弄。你应该首先从不手动复制/下载/移动/包括个人servletcontainer专用库喜欢servlet-api.jarjsp-api.jarel-api.jarj2ee.jarjavaee.jar,等它只会导致未来的可移植性,兼容性,类路径和可维护性的烦恼,因为你的web应用程序会当它是不工作部署到与最初获得这些库的地方不同的制造商/版本的 servletcontainer。

In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>.

如果您使用 Maven,则需要绝对确保目标运行时已提供的特定于 servletcontainer 的库标记为<scope>provided</scope>.

Here are some typical exceptions which you can get when you litter the /WEB-INF/libor even /JRE/lib, /JRE/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:

下面是一些典型的例外,你可以当你的垃圾/WEB-INF/lib,甚至/JRE/lib/JRE/lib/ext等在一个不小心尝试修复编译错误servletcontainer专用库:

回答by Juha Syrj?l?

From wikipedia.

来自维基百科

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                "Transitional//EN\">\n" +
                "<html>\n" +
                "<head><title>Hello WWW</title></head>\n" +
                "<body>\n" +
                "<h1>Hello WWW</h1>\n" +
                "</body></html>");
  }
}

This, of course, works only if you have added the servlet-api.jarto Eclipse build path. Typically your application server (e.g Tomcat) will have the right jar file.

当然,这仅在您已添加servlet-api.jar到 Eclipse 构建路径时才有效。通常,您的应用程序服务器(例如Tomcat)将具有正确的 jar 文件。

回答by prayagupd

import javax.servlet

import javax.servlet

STEP 1

第1步

Go to propertiesof your project ( with Alt+Enteror righ-click )

转到项目的属性(使用Alt+Enter或右键单击)

STEP 2

第2步

check on Apache Tomcat v7.0under Targeted Runtimeand it works.

Targeted Runtime下检查Apache Tomcat v7.0,它可以工作。

回答by Hari Gudigundla

Quick Fix- This worked in Eclipse - Right Click on project -> Properties -> Java Build Path (Tab) -> Add External JARs -> locate the servlet api jar implementation (if Tomcat - its named servlet-api.jar) -> click OK. That's it !!

快速修复 - 这在 Eclipse 中有效 - 右键单击​​项目 -> 属性 -> Java 构建路径(选项卡) -> 添加外部 JAR -> 找到 servlet api jar 实现(如果 Tomcat - 它名为 servlet-api.jar) ->单击确定。就是这样 !!

回答by ivanLam

Little bit difference from Hari:

与 Hari 的一点区别:

Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.

右键单击项目 ---> 属性 ---> Java 构建路径 ---> 添加库... ---> 服务器运行时 ---> Apache Tomcat ----> 完成。

回答by Ivan Vu?ica

I was getting a null pointer exception during project creation related to "Dynamic Web Module".

在与“动态 Web 模块”相关的项目创建过程中,我遇到了空指针异常。

To get the project to compile (that is, to javax.servletto import successfully) I had to go to project's Properties, pick Project Facetsin the sidebar, tick Dynamic Web Moduleand click Apply.

要编译项目(即javax.servlet成功导入),我必须转到项目的Properties,在侧栏中选择Project Facets,勾选Dynamic Web Module并单击Apply

Surprisingly, this time "Dynamic Web Module" facet installed correctly, and import started to work.

令人惊讶的是,这次“动态 Web 模块”方面安装正确,并且导入开始工作。

回答by Mitul Maheshwari

you can simply copy the servlet-api.jarand copy that jar files into lib folder, which is in WEB-INF. then just clean and built your project, your errors will be solved.

您可以简单地将servlet-api.jarjar 文件复制并复制到 WEB-INF 中的 lib 文件夹中。然后只需清理并构建您的项目,您的错误就会得到解决。

  **OR**

you can directly add jar files to library by using following steps.

您可以使用以下步骤直接将 jar 文件添加到库中。

  1. Right click on project.
  2. Go To Properties.
  3. Go to Java Build Path.
  4. Select Add Library option from tabs.
  5. Add Jar Files
  6. give path of your servlet-api.jarfile.
  7. Clean and build your project.
  1. 右键单击项目。
  2. 转到属性。
  3. 转到 Java 构建路径。
  4. 从选项卡中选择添加库选项。
  5. 添加 Jar 文件
  6. 给你的servlet-api.jar文件路径。
  7. 清理并构建您的项目。

回答by RuntimeException

You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar

最重要的是,您永远不要手动复制/下载/移动/包含单个 servletcontainer 特定的库,例如 servlet-api.jar

@BalusC,

@BalusC,

I would prefer to use the exact classes that my application is going to use rather than one provided by Eclipse (when I am feeling like a paranoid developer).

我更愿意使用我的应用程序将要使用的确切类,而不是 Eclipse 提供的类(当我感觉自己像个偏执的开发人员时)。

Another solution would be to use Eclipse "Configure Build Path" > Libraries > Add External Jars, and add servlet api of whatever Container one chooses to use.

另一种解决方案是使用 Eclipse“配置构建路径”>“库”>“添加外部罐子”,并添加选择使用的任何容器的 servlet api。

And follow @kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home. However it introduces another constraint that the developer must install Weblogic on his/her local machine if weblogic is being used ! Any other cleaner solution?

并在使用 ant 构建时遵循 @kaustav datta 的解决方案 - 拥有像 tomcat.home 或 weblogic.home 这样的属性。然而,它引入了另一个约束,如果正在使用 weblogic,开发人员必须在他/她的本地机器上安装 Weblogic!还有其他更清洁的解决方案吗?

回答by sarath

Include servlet-api.jar from your server lib folder.enter image description here

从您的服务器 lib 文件夹中包含 servlet-api.jar。enter image description here

Do this step

做这一步

enter image description here

enter image description here

回答by Paul LeBeau

In my case, when I went to the Targetted Runtimes, screen, Tomcat 7 was not listed (disabled) despite being installed.

就我而言,当我转到Targetted Runtimes屏幕时,尽管已安装 Tomcat 7,但并未列出(禁用)。

To fix, I had to go to Preferences->Server->Runtime Environmentsthen uninstall and reinstall Tomcat 7.

要修复,我必须转到首选项->服务器->运行时环境,然后卸载并重新安装 Tomcat 7。