The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法

目录

 

错误描述及分析

解决方法一:在pom.xml中配置

解决方法二:添加tomcat运行环境

1的设置步骤:

2的配置步骤:


  • 错误描述及分析


     

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法

使用eclipse开发web程序搭建环境时出现【The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path】错误,字面翻译为:在工程的编译路径中无法找到javax.servlet.http.HttpServlet基类,猜测此错误的原因可能在于我们下载的eclipse为java se开发版本而非java ee 版本的原因,不具有web的依赖包。而javax.servlet.http.HttpServlet是web容器处理类,在tomcat程序目录下具有此包servlet-api.jar,此包包含此类的定义。根据总结,解决思路为:导入此依赖环境所在的jar包

  • 解决方法一:在pom.xml中配置


查询maven库获取到最新的servlet-api.jar版本信息,配置在项目的pom.xml文件中,直接解决错误问题。配置如下所示:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法

  • 解决方法二:添加tomcat运行环境


此方法与方法一比较起来稍显复杂,网上大多也充斥着这样的解决方法,其解决方式分为两步:

  1. 配置eclipse的tomcat位置
  2. 配置工程build path,将tomcat运行环境加入到工程lib目录

1的设置步骤:

在eclipse中选择菜单Window > Preference > Server > Runtime Envirionments > Add,然后如下图操作选择自己的tomcat 安装位置进行配置,最后 finish > Applay and close。

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
add选择添加

 

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
选择自己安装的tomcat版本

 

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
选择对应版本安装在本机的tomcat位置

2的配置步骤:

右键工程属性 > Java Build Path > Add Libary > Server Runtime > tomcatx > finish > Applay and close

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
Add Library

 

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
Server Runtime

 

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误的两种解决方法
选择1中添加的tomcat版本

最后 finish > Applay and close,问题解决。