Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
titleFixing "The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path"

Eclipse might show you an error marker on your project, claiming that "The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path". You can fix this by opening the properties of your project (right click on your project > Properties). Then click on "Project Facets" and from the facet list select "Dynamic Web Module." On the right, click on "Runtimes" and select "Apache Tomcat v8.0".

By adding target runtime to the project, http-servlet will be available to the project class-path.

Alternatively, you can fix this by adding javax.servlet.jsp-api to the pom and mark the dependency as provided

Code Block
languagexml
titlepom.xml
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
</dependency>


Let Spring take over

Now, you are all setup to start with Spring. First, you need to tell Tomcat that you want Spring to handle incoming requests. You do this by specifying which servlet Tomcat should use as 'entry point' into your application. Open the file web.xml located in src > main > webapp > WEB-INF. Add the following code inside the 'web-app' tag after the 'display-name' section:

...

Code Block
<web-app id="WebApp_ID" version="3.1"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

After adding the web-app tag with schema, if you face any issue in your eclipse like "Download from external resource is disabled", you can enable it by Go to top bar: Window → Preferences → Maven → Check the option  ("Download Artifact javadoc"). Then apply and close.

What did you just do? First, you specified a servlet that Tomcat should know (the 'servlet' section). You named the servlet 'dispatcher' and you told Tomcat that you wanted it to be of type 'org.springframework.web.servlet.DispatcherServlet'. You also told Tomcat to initialize the dispatcher servlet with a file named servlet-context.xml located in WEB-INF > spring > appServlet.

...