Eclipse:如何将 javax.servlet 包添加到项目中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4213644/
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-09-10 19:44:47  来源:igfitidea点击:

Eclipse: How do I add the javax.servlet package to a project?

eclipseservlets

提问by Tony the Pony

I'm using Eclipse 3.6 Helios (for Java Developers) and want to add the javax.servlet package to one of my projects. What do I need to configure/download?

我正在使用 Eclipse 3.6 Helios(适用于 Java 开发人员)并希望将 javax.servlet 包添加到我的项目之一。我需要配置/下载什么?

回答by Michael Reed

To expound on darioo's answer with a concrete example. Tomcat 7 installed using homebrew on OS X, using Eclipse:

用一个具体的例子来阐述darioo的答案。Tomcat 7 在 OS X 上使用自制软件安装,使用 Eclipse:

  1. Right click your project folder, select Properties at the bottom of the context menu.
  2. Select "Java Build Path"
  3. Click Libraries" tab
  4. Click "Add Library..." button on right (about halfway down)
  5. Select "Server Runtime" click "Next"
  6. Select your Tomcat version from the list
  7. Click Finish
  1. 右键单击您的项目文件夹,选择上下文菜单底部的属性。
  2. 选择“Java构建路径”
  3. 单击“库”选项卡
  4. 单击右侧的“添加库...”按钮(大约向下一半)
  5. 选择“服务器运行时”点击“下一步”
  6. 从列表中选择您的 Tomcat 版本
  7. 单击完成

What? No Tomcat version is listed even though you have it installed via homebrew??

什么?即使您通过自制软件安装了它,也没有列出 Tomcat 版本??

  1. Switch to the Java EE perspective (top right)
  2. In the "Window" menu select "Show View" -> "Servers"
  3. In the Servers tab (typically at bottom) right click and select "New > Server"
  4. Add the path to the homebrew tomcat installation in the dialog/wizard (something like: /usr/local/Cellar/tomcat/7.0.14/libexec)
  1. 切换到 Java EE 透视图(右上角)
  2. 在“窗口”菜单中选择“显示视图”->“服务器”
  3. 在服务器选项卡中(通常在底部)右键单击并选择“新建 > 服务器”
  4. 在对话框/向导中添加自制 tomcat 安装的路径(类似于:/usr/local/Cellar/tomcat/7.0.14/libexec)

Hope that helps someone who is just getting started out a little.

希望对刚入门的人有所帮助。

回答by darioo

Right click on your project -> properties -> build path. Add to your build path jarfile(s) that have the javax.servletimplemenation. Ite depends on your servlet container or application server what file(s) you need to include, so search for that information.

右键单击您的项目 -> 属性 -> 构建路径。添加到jar具有javax.servlet实现的构建路径文件中。这取决于您的 servlet 容器或应用程序服务器需要包含哪些文件,因此请搜索该信息。

回答by Gábor Lipták

When you define a server in server view, then it will create you a server runtime library with server libs (including servlet api), that can be assigned to your project. However, then everybody that uses your project, need to create the same type of runtime in his/her eclipse workspace even for compiling.

当您在服务器视图中定义服务器时,它将为您创建一个带有服务器库(包括 servlet api)的服务器运行时库,该库可以分配给您的项目。但是,使用您的项目的每个人都需要在他/她的 eclipse 工作区中创建相同类型的运行时,即使是为了编译。

If you directly download the servlet api jar, than it could lead to problems, since it will be included into the artifacts of your projects, but will be also present in servlet container.

如果您直接下载 servlet api jar,则可能会导致问题,因为它将包含在您的项目的工件中,但也会出现在 servlet 容器中。

In Maven it is much nicer, since you can define the servlet api interfaces as a "provided" dependency, that means it is present in the "to be production" environment.

在 Maven 中,它要好得多,因为您可以将 servlet api 接口定义为“提供的”依赖项,这意味着它存在于“待生产”环境中。

回答by Stefano Carfagna

Go to

JBoss\jboss-eap-6.1\modules\system\layers\base\javax\servlet\api\main

JBoss\jboss-eap-6.1\modules\system\layers\base\javax\servlet\api\main

include JAR

包括 JAR

jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar

jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar

For me it worked

对我来说它有效

回答by MildaGenius

For me doesnt put jars to lib directory and set to Build path enought.

对我来说,没有将 jars 放入 lib 目录并设置为 Build path 足够了。

The right thing was add it to Deployment Assembly.

正确的做法是将其添加到部署程序集。

Original asnwer

原答案

回答by Mohamed Saligh

  1. Download the file from http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadjavaxservletjar.htm

  2. Make a folder ("lib") inside the project folder and move that jar file to there.

  3. In Eclipse, right click on project > BuildPath > Configure BuildPath > Libraries > Add External Jar

  1. http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadjavaxservletjar.htm下载文件

  2. 在项目文件夹中创建一个文件夹(“lib”)并将该 jar 文件移动到那里。

  3. 在 Eclipse 中,右键单击项目 > BuildPath > Configure BuildPath > Libraries > Add External Jar

Thats all

就这样