Home>
  • I want to compile a java file like the one below.
package classes;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet (urlPatterns = {"/ jsp/index"})
public class Hello extends HttpServlet {
    public void doGet (HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
        PrintWriter out = response.getWriter ();
        out.println ("Hello");
        out.println (new java.util.Date ());
    }
}


However, I get an error message like this even though I don't see any compilation errors.

% javac webapps/ROOT/WEB-INF/classes/Hello.java
webapps/ROOT/WEB-INF/classes/Hello.java: 6: Error: Package javax.servlet does not exist
import javax.servlet.ServletException;
                    ^ ^
webapps/ROOT/WEB-INF/classes/Hello.java: 7: Error: Package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;
                         ^ ^
webapps/ROOT/WEB-INF/classes/Hello.java: 8: Error: Package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
                         ^ ^
webapps/ROOT/WEB-INF/classes/Hello.java: 9: Error: Package javax.servlet.http does not exist
import javax.servlet.http.HttpServletResponse;
                         ^ ^
webapps/ROOT/WEB-INF/classes/Hello.java:11: Error: Symbol not found
public class Hello extends HttpServlet {
                           ^ ^
  Symbol: Class HttpServlet
webapps/ROOT/WEB-INF/classes/Hello.java: 10: Error: Symbol not found
@WebServlet (urlPatterns = {"/ jsp/index"})
 ^ ^
  Symbol: Class WebServlet
webapps/ROOT/WEB-INF/classes/Hello.java:12: Error: Symbol not found
    public void doGet (HttpServletRequest request, HttpServletResponse response)
                      ^ ^
  Symbol: Class HttpServletRequest
  Location: Class Hello
webapps/ROOT/WEB-INF/classes/Hello.java:12: Error: Symbol not found
    public void doGet (HttpServletRequest request, HttpServletResponse response)
                                                  ^ ^
  Symbol: Class HttpServletResponse
  Location: Class Hello
webapps/ROOT/WEB-INF/classes/Hello.java:13: Error: Symbol not found
                    throws ServletException, IOException {
                           ^ ^
  Symbol: Class ServletException
  Location: Class Hello
9 errors
environment
  • tomcat9.0.38
  • jdk11
  • macOS 10.15

It may be basic, but I would appreciate it if i am a beginner in programming.

  • Answer # 1

    To build (compile) a Java web application, you need a jar file of the Servlet API that matches the Servlet container (Tomcat in this case) you want to run.

    However, the procedure is very complicated, so if there are no particular restrictions, creating an environment using an IDE such as Eclipse, IntelliJ, or NetBeans will make it very easy to create an environment, so create a web application using that. It is desirable to do.

    Personally, the JRE version of Pleiades All in one Eclipse below is a good choice.