eclipse ServletException、HttpServletResponse 和 HttpServletRequest 无法解析为类型

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

ServletException, HttpServletResponse and HttpServletRequest cannot be resolved to a type

eclipseservletsjakarta-eecompiler-errors

提问by BAlu BAlaji

I got an error while creating a servlet in eclipse like

在 Eclipse 中创建 servlet 时出现错误,例如

Multiple markers at this line

  • ServletException cannot be resolved to a type
  • HttpServletResponse cannot be resolved to a type
  • HttpServletRequest cannot be resolved to a type

此行有多个标记

  • ServletException 无法解析为类型
  • HttpServletResponse 无法解析为类型
  • HttpServletRequest 无法解析为类型

回答by Areeg Samir

You can do the folllwoing: import the jar file inside you class:

您可以执行以下操作:在类中导入 jar 文件:

import javax.servlet.http.HttpServletResponse

import javax.servlet.http.HttpServletResponse

add the Apache Tomcat library as follow:

添加Apache Tomcat库如下:

Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 6.0 > Finish > Ok

项目 > 属性 > Java 构建路径 > 库 > 从库选项卡添加库 > 选择服务器运行时 > 下一步 > 选择 Apache Tomcat v 6.0 > 完成 > 确定

Also First of all, make sure that Servlet jar is included in your class path in eclipse as PermGenError said.

另外首先,请确保 Servlet jar 包含在 eclipse 中的类路径中,如 PermGenError 所说。

I think this will solve your error

我认为这将解决您的错误

回答by PermGenError

Two possible issues could be

两个可能的问题可能是

  • you either forgot to include Servlet jar in your classpath
  • you forgot to import it in your Servlet class
  • 您要么忘记在类路径中包含 Servlet jar
  • 你忘了在你的 Servlet 类中导入它

To include Servlet jar in your class path in eclipse, Download the latest Servlet Jarand configure using buildpath option. look at this Linkfor more info.

要在 Eclipse 的类路径中包含 Servlet jar,请下载最新的Servlet Jar并使用 buildpath 选项进行配置。查看此链接了解更多信息。

If you have included the jar make sure that your import is declared.

如果您已包含 jar,请确保已声明您的导入。

import javax.servlet.http.HttpServletResponse

回答by Chinmoy

Select Tomcat server in Targeted Runtime

在 Targeted Runtime 中选择 Tomcat 服务器

Project->Properties->Targeted Runtimes (Select your Tomcat Server)

Project->Properties->Targeted Runtimes(选择你的Tomcat服务器)

回答by Lokesh Tiwari

Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 7.0> Finish > Ok

项目 > 属性 > Java 构建路径 > 库 > 从库选项卡添加库 > 选择服务器运行时 > 下一步 > 选择 Apache Tomcat v 7.0 > 完成 > 确定

回答by Kevin Bowersox

Are the classes imported? Try pressing CTRL+ SHIFT+ Oto resolve the imports. If this does not work you need to include the application servers runtime libraries.

类是进口的吗?尝试按CTRL+ SHIFT+O来解决导入问题。如果这不起作用,您需要包含应用程序服务器运行时库。

  1. Windows > Preferences
  2. Server > Runtime Environment
  3. Add
  4. Select your appropriate environment, click Next
  5. Point to the install directory and click Finish.
  1. 窗口 > 首选项
  2. 服务器 > 运行时环境
  3. 添加
  4. 选择合适的环境,点击下一步
  5. 指向安装目录并单击完成。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Brimstedt

if you are using maven:

如果您使用的是 Maven:

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
</dependency>

回答by ?MER TA?CI

As a reason of this problem, some code is broken or undefined.You may see an error in a java class such as "The type javax.servlet.http.HttpSession cannot be resolved. It is indirectly referenced from required .class files". you shuold download " javax.servlet.jar"as mentioned before. Then configure your project build path, add the javax.servlet.jar as an external jar.I hope it fixes the problem.At least it worked for me.

作为此问题的原因,某些代码已损坏或未定义。您可能会在 java 类中看到错误,例如“无法解析类型 javax.servlet.http.HttpSession。它是从所需的 .class 文件间接引用的”。如前所述,您应该下载“ javax.servlet.jar”。然后配置您的项目构建路径,将 javax.servlet.jar 添加为外部 jar。我希望它可以解决问题。至少它对我有用。