Hi all,

I am trying to compile a servlet, and am getting the following error.

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\flatironswp1\WEB-INF\classes>javac export\ExportUtil.java
export\ExportUtil.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^

and here's my code:

package export;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class ExportUtil extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("blah, blah...!");
    out.close();
  }
}

Do I need any jars or something in my classpath? please suggest.


Thanks.

Yes, those components are not part of the Java SE API so you will need to provide classpath entries to the locations of the jars containing the Java EE api implementations. There should be some info in your Tomcat docs on how to compile those source files.

This article may be helpful as well. http://www.sitepoint.com/article/java-servlets-1/3

package javax.servlet does not exist
This is my problem can any one clear the error and tell any solutions.here is my code
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.Http.*;
import java.sql.*;

public class Callid extends HttpServlet {


//*****  Servlet access to data base


    public void doGet (HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{

    	String url   = "jdbc:mysql://localhost:3306/asterisk";

    	String query = "SELECT * FROM cdr WHERE dst = '5102'";
						
	try {

		Class.forName  ("com.mysql.jdbc.Driver");
      		//Class.forName("org.gjt.mm.mysql.Driver");

      		Connection con = DriverManager.getConnection 
		  ( url, "root", "" );

            Statement stmt = con.createStatement ();

            ResultSet rs = stmt.executeQuery (query);

            printResultSet ( resp, rs );

            rs.close();
            stmt.close();
            con.close();

        }  // end try

        catch (SQLException ex) {
            
		PrintWriter out = resp.getWriter();
	        resp.setContentType("text/html");
			
		while (ex != null) {  
                	out.println ("SQL Exception:  " + ex.getMessage ());
                	ex = ex.getNextException ();  
              }  // end while

        }  // end catch SQLException

        catch (java.lang.Exception ex) {

      	PrintWriter out = resp.getWriter();
		resp.setContentType("text/html");	
		out.println ("Exception:  " + ex.getMessage ());
	  }

    }  // end doGet


    private void printResultSet (HttpServletResponse resp, ResultSet rs )
        throws SQLException  {

        try  {

		PrintWriter out = resp.getWriter();

	        out.println("<html>");
	        out.println("<head><title>jbs jdbc/mysql servlet</title></head>");
	        out.println("<body>");
	        out.println("<center><font color=AA0000>");
	        out.println("<h3>jbsJDBCServlet</h3>");
	        out.println("<h3>Logged In Data</h3>");
	        
	        out.println("<table border='1'>");

           	  int numCols = rs.getMetaData().getColumnCount();
              while ( rs.next() ) 
		  {
		  out.println("<tr>");
               	  for (int i=1; i<=numCols; i++) {
                    out.print("<td>" + rs.getString(i) + "</td>" );
              }  // end for
                  out.println("</tr>");
              }  // end while

	        out.println("</table>");
	        
	        out.println("</font></center>");
	        out.println("</body>");
	        out.println("</html>");
	        out.close();

	    }  // end try
        catch ( IOException except)  {
        }  // end catch

    }  // end returnHTML


}  // end jbsJDBCServlet

You oviously missed this part of forum

When posting programming code, encase it in (code), (code=syntax), where 'syntax' is any language found within our Code Snippets section, or (icode), for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework.

Plus error messages that you getting would be helpful instead of us going through your coding and trying to eliminate various mistakes you made

The thing which helped me:
In Eclipse: Project -> Properties -> Java Build Path -> add Libraries
Click on "Add Library" and select Server Runtime (I am using Geronimo 2.0)

3 1/2 years old post...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.