导入javax.servlet 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4119448/
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
The import javax.servlet can't be resolved
提问by snakile
I'm trying to use eclipsefor Java EEto develop web applications.
我正在尝试使用Eclipsefor Java EE来开发 Web 应用程序。
I need to use Tomcatas my server. I've downloaded Tomcat and it's running. But my program doesn't compile.
我需要使用Tomcat作为我的服务器。我已经下载了 Tomcat 并且它正在运行。但是我的程序无法编译。
I get the following error:
我收到以下错误:
The import javax.servlet can't be resolved.
导入 javax.servlet 无法解析。
What do I need to do?
我需要做什么?
采纳答案by Richard Fearn
You need to add the Servlet API to your classpath. In Tomcat 6.0, this is in a JAR called servlet-api.jar
in Tomcat's lib
folder. You can either add a reference to that JAR to the project's classpath, or put a copy of the JAR in your Eclipse project and add it to the classpath from there.
您需要将 Servlet API 添加到您的类路径中。在 Tomcat 6.0 中,这是servlet-api.jar
在 Tomcatlib
文件夹中调用的 JAR中。您可以将对该 JAR 的引用添加到项目的类路径中,也可以将 JAR 的副本放入 Eclipse 项目中,然后从那里将其添加到类路径中。
If you want to leave the JAR in Tomcat's lib
folder:
如果要将 JAR 留在 Tomcat 的lib
文件夹中:
- Right-click the project, click Properties.
- Choose Java Build Path.
- Click the Librariestab
- Click Add External JARs...
- Browse to find
servlet-api.jar
and select it. - Click OKto update the build path.
- 右键单击该项目,单击Properties。
- 选择Java 构建路径。
- 单击库选项卡
- 单击添加外部 JAR...
- 浏览以查找
servlet-api.jar
并选择它。 - 单击“确定”以更新构建路径。
Or, if you copy the JAR into your project:
或者,如果您将 JAR 复制到您的项目中:
- Right-click the project, click Properties.
- Choose Java Build Path.
- Click Add JARs...
- Find
servlet-api.jar
in your project and select it. - Click OKto update the build path.
- 右键单击该项目,单击Properties。
- 选择Java 构建路径。
- 单击添加 JAR...
servlet-api.jar
在您的项目中找到并选择它。- 单击“确定”以更新构建路径。
回答by Bozho
Add the servlet-api.jar
to your classpath. You can take it from tomcat's lib folder.
添加servlet-api.jar
到您的类路径。您可以从 tomcat 的 lib 文件夹中获取它。
回答by BalusC
If not done yet, you need to integrate Tomcat in your Serversview. Rightclick there and choose New > Server. Select the appropriate Tomcat version from the list and complete the wizard.
如果还没有完成,您需要将 Tomcat 集成到您的服务器视图中。右键单击那里并选择New > Server。从列表中选择适当的 Tomcat 版本并完成向导。
When you create a new Dynamic Web Project, you should select the integrated server from the list as Targeted Runtimein the 1st wizard step.
当您创建一个新的动态 Web 项目时,您应该在第一个向导步骤中从列表中选择集成服务器作为目标运行时。
Or when you have an existing Dynamic Web Project, you can set/change it in Targeted Runtimesentry in project's properties. Eclipse will then automagically add all its libraries to the build path (without having a copy of them in the project!).
或者,当您有一个现有的Dynamic Web Project 时,您可以在项目属性的Targeted Runtimes条目中设置/更改它。然后 Eclipse 会自动将其所有库添加到构建路径中(项目中没有它们的副本!)。
回答by YMW
Had the same problem in Eclipse. For some reason I didn't have the servlet.jar file in my build path. What I wound up doing was copying a "lib" folder from another project of mine to the project I was working on, then manually going into that folder and adding the servlet.jar file to the build path (option shows up when you right-click on the file in the project explorer).
在 Eclipse 中遇到了同样的问题。出于某种原因,我的构建路径中没有 servlet.jar 文件。我最后做的是将我的另一个项目中的“lib”文件夹复制到我正在处理的项目中,然后手动进入该文件夹并将 servlet.jar 文件添加到构建路径中(当您正确时显示选项 -单击项目资源管理器中的文件)。
回答by Jeroen Rondeel
You need to set the scope of the dependency to 'provided' in your POM.
您需要在 POM 中将依赖项的范围设置为“提供”。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
Then everything will be fine.
那么一切都会好起来的。
回答by leiseliesel
I had the same problem because my "Dynamic Web Project" had no reference to the installed server i wanted to use and therefore had no reference to the Servlet API the server provides.
我遇到了同样的问题,因为我的“动态 Web 项目”没有引用我想要使用的已安装服务器,因此没有引用服务器提供的 Servlet API。
Following steps solved it without adding an extra Servlet-API to the Java Build Path (Eclipse version: Luna):
以下步骤解决了它,而无需向 Java 构建路径(Eclipse 版本:Luna)添加额外的 Servlet-API :
- Right clickon your "Dynamic Web Project"
- Select Properties
- Select Project Facetsin the list on the left side of the "Properties" wizard
- On the right side of the wizard you should see a tab named Runtimes. Select the Runtime tab and check the serveryou want to run the servlet.
- 右键单击您的“动态 Web 项目”
- 选择属性
- 在“属性”向导左侧的列表中选择Project Facets
- 在向导的右侧,您应该会看到一个名为Runtimes的选项卡。选择 Runtime 选项卡并选中要运行 servlet的服务器。
Edit:if there is no server listed you can create a new one on the Runtimes tab
编辑:如果没有列出服务器,您可以在“运行时”选项卡上创建一个新服务器
回答by Varun
If you get this compilation error, it means that you have not included the servlet jar in the classpath. The correct way to include this jar is to add the Server Runtime jar to your eclipse project. You should follow the steps below to address this issue: You can download the servlet-api.jar from here http://www.java2s.com/Code/Jar/s/Downloadservletapijar.htm
如果您收到此编译错误,则表示您尚未在类路径中包含 servlet jar。包含此 jar 的正确方法是将 Server Runtime jar 添加到您的 eclipse 项目中。您应该按照以下步骤来解决这个问题: 您可以从这里下载 servlet-api.jar http://www.java2s.com/Code/Jar/s/Downloadservletapijar.htm
Save it in directory. Right click on project -> go to properties->Buildpath and follow the steps.
Note:The jar which are shown in the screen are not correct jar.
注意:屏幕上显示的罐子不是正确的罐子。
you can follow the step to configure.
您可以按照步骤进行配置。
回答by Andrey
Add to pom.xml
添加到 pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>