Adding a servlet in IntelliJ, says package javax.servlet.http does not exist

18,381
  • You are missing the servlet-api.jar on your build path.
  • jsp's are picked automatically without the need to register them. web.xml is for servlets, filters, listeners and other settings.
  • Don't use the default package. Always give a package to your classes. So it better be test.TestServlet
Share:
18,381
Blankman
Author by

Blankman

... .. . blank

Updated on June 07, 2022

Comments

  • Blankman
    Blankman almost 2 years

    Just creating my first IntelliJ web application that runs on tomcat.

    The project ran fine, and it rendered the index.jsp just fine.

    How come the index.jsp rendered even though the web.xml doesn't have a reference to it btw? Does it first look for psychical files (.jsp's), if present, it executes them? Or is web.xml just for servlets?

    The real issue was I created a TestServlet in my /src folder, and it can't seem to find the javax.servlet jar:

    package javax.servlet.http does not exist
    

    Reference:

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
               version="2.5">
    
        <servlet>
            <display-name>Test1</display-name>
            <servlet-name>TestServlet</servlet-name>
            <servlet-class>TestServlet</servlet-class>
        </servlet>
    </web-app>
    

    TestServlet.java:

    import java.io.IOException;
    
    package hello_world.Servlets
    
    /**
     * Created by IntelliJ IDEA.
     * User: snad
     * Date: Oct 29, 2011
     * Time: 9:19:27 AM
     * To change this template use File | Settings | File Templates.
     */
    public class TestServlet extends javax.servlet.http.HttpServlet {
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    
        }
    
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    
        }
    }