5 Replies Latest reply on Nov 28, 2006 8:00 PM by 807581

    Cannot fing the javax.servlet.*; package

    807581
      Hi everybody.. I am new to J2EE Programming.. I just wrote a very small and simple Servlet to see how it works.. The Servlet is designed to take in the the name and a favourite color of a user via a HTML page and display it to him
      Here is the code:

      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      public class VerifyData extends HttpServlet
      {
           public void doGet (HttpServletRequest reg, HttpServletResponse res) throws ServletException, IOException
           {
                res.setContentType("text/html");
                PrintWriter out = res.getWriter();
                String name= req.getParameter ("name");
                String color=req.getParameter ("color");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Simple HTML Form</title>");
                out.println("<link rel='styleSheet' type='text/css' href='examples.css'>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Hello "+name+" Your favourite Color is : " +color+"</h1>");
                out.println("<form method='GET' action='verifydata'>");
                out.println("<p>Please Type in your name here:</p>");
                out.println("<input type='text' name='name'>");
                out.println("<p>Now select the color of your choice</p>");
                out.println("<select name='color' size='1'>");
                out.println("<option>red<option>green<option>blue</select>");
                out.println("<br></br>");
                out.println("<input type='submit'>");
                out.println("<br></br>");
                out.println("<table>");
                out.println("<tr><th>Colors</th><th>Description</th></tr>");
                out.println("<tr><td>Red</td><td>Blood</td></tr>");
                out.println("<tr><td>Green</td><td>Grass</td></tr>");
                out.println("<tr><td>Blue</td><td>Sky</td></tr>");
                out.println("</table>");
                out.println("</form>");
                out.println("</body>");
                out.println("</html>");
                
           }
      }
      BUT I got the following Error:

      C:\Sun\AppServer\jdk\bin>javac VerifyData.java
      VerifyData.java:2: package javax.servlet does not exist
      import javax.servlet.*;
      ^
      VerifyData.java:3: package javax.servlet.http does not exist
      import javax.servlet.http.*;
      ^
      VerifyData.java:5: cannot find symbol
      symbol: class HttpServlet
      public class VerifyData extends HttpServlet
                                      ^
      VerifyData.java:7: cannot find symbol
      symbol  : class HttpServletRequest
      location: class VerifyData
              public void doGet (HttpServletRequest reg, HttpServletResponse res) thro
      ws ServletException, IOException
                                 ^
      VerifyData.java:7: cannot find symbol
      symbol  : class HttpServletResponse
      location: class VerifyData
              public void doGet (HttpServletRequest reg, HttpServletResponse res) thro
      ws ServletException, IOException
                                                         ^
      VerifyData.java:7: cannot find symbol
      symbol  : class ServletException
      location: class VerifyData
              public void doGet (HttpServletRequest reg, HttpServletResponse res) thro
      ws ServletException, IOException
      
         ^
      VerifyData.java:11: cannot find symbol
      symbol  : variable req
      location: class VerifyData
                      String name= req.getParameter ("name");
                                   ^
      VerifyData.java:12: cannot find symbol
      symbol  : variable req
      location: class VerifyData
                      String color=req.getParameter ("color");
                                   ^
      8 errors
      
      C:\Sun\AppServer\jdk\bin>
      Please please please help me out. I am not sure about the class path settings etc.. but its like this C:\Sun\AppServer\jdk\lib\tools.jar
        • 1. Re: Cannot fing the javax.servlet.*; package
          807581
          Package you are looking for is in C:\Sun\AppServer\lib\javaee.jar (if you are using Java EE 5) or in C:\Sun\AppServer\lib\j2ee.jar (if you are using J2EE 1.4). So, simply add that jar to your compilation classpath.
          • 2. Re: Cannot fing the javax.servlet.*; package
            807581
            Thanks a lot :) Its working fine.
            • 3. Cannot find symbol HttpServlet
              807581
              Hi everybody

              I am new to j2ee.I am using java ee5.Application server as apache-tomcat-5.5.17.I tried to compile servlet using javac.it gives the following errors.

              HelloServlet2.java:4: package javax.servlet does not exist
              import javax.servlet.*;
              ^
              HelloServlet2.java:5: package javax.servlet.http does not exist
              import javax.servlet.http.*;
              ^
              HelloServlet2.java:15: cannot find symbol
              symbol: class HttpServlet
              public class HelloServlet2 extends HttpServlet {
              ^
              HelloServlet2.java:16: cannot find symbol
              symbol : class HttpServletRequest
              location: class coreservlets.HelloServlet2
              public void doGet(HttpServletRequest request,
              ^
              HelloServlet2.java:17: cannot find symbol
              symbol : class HttpServletResponse
              location: class coreservlets.HelloServlet2
              HttpServletResponse response)
              ^
              HelloServlet2.java:18: cannot find symbol
              symbol : class ServletException
              location: class coreservlets.HelloServlet2
              throws ServletException, IOException {
              ^
              6 errors

              this is the code

              public class HelloServlet extends HttpServlet {
              public void doGet(HttpServletRequest request,
              HttpServletResponse response)
              throws ServletException, IOException {
              response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              String docType =
              "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
              "Transitional//EN\">\n";
              out.println(docType +
              "<HTML>\n" +
              "<HEAD><TITLE>Hello (2)</TITLE></HEAD>\n" +
              "<BODY BGCOLOR=\"#FDF5E6\">\n" +
              "<H1>Hello (2)</H1>\n" +
              "</BODY></HTML>");
              }
              }

              pls help me to resolve this problem.

              Thank you
              sam
              • 4. Re: Cannot fing the javax.servlet.*; package
                807581
                Hi

                This problem happens to me also. My classpath is set up as you indicates and my path is also, but when I compile my servlet using next sentence, it works fine.

                javac -classpath "D:\Tomcat\common\lib\servlet.jar;C:\Sun\AppServer\lib\j2ee.jar" BeerSelect.java

                I would like it to be compiled without using such a long sentence or writing a *.bat for every new servlet I create. Is there a way to do this?

                Thanks in advanced.
                • 5. Re: Cannot fing the javax.servlet.*; package
                  807581
                  hi,

                  on windows try to set up de classpath on Start - Control Panel - System - Advanced - Environment Variables - System Variables and just create a variable called CLASSPATH.

                  i think you can also create an bat file with SET CLASSPATH=D:\Tomcat\common\lib\servlet.jar