HttpServlet cannot be resolved to a type

Hey everyone,

Im trying to get a servlet running but I get the following errors

javax.servlet.ServletException: Error instantiating servlet class SimpleXSLTServlet

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:818)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:624)

org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)

java.lang.Thread.run(Unknown Source)

java.lang.Error: Unresolved compilation problems:

HttpServlet cannot be resolved to a type

ServletConfig cannot be resolved to a type

ServletException cannot be resolved to a type

HttpServletRequest cannot be resolved to a type

HttpServletResponse cannot be resolved to a type

ServletException cannot be resolved to a type

The method getServletContext() is undefined for the type SimpleXSLTServlet

SimpleXSLTServlet.<init>(SimpleXSLTServlet.java:23)

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

java.lang.reflect.Constructor.newInstance(Unknown Source)

java.lang.Class.newInstance0(Unknown Source)

java.lang.Class.newInstance(Unknown Source)

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)

org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:818)

org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:624)

org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)

java.lang.Thread.run(Unknown Source)

I already included the servlet/api.jar in the build path, but i dont understand why its not working with that. Im running a tomcat 6.0.2.

any help would be appreciated!

thanks

[2226 byte] By [javamajaa] at [2007-11-26 14:58:55]
# 1
you need to get the file to compile before it will runlooks like import problems to mepost the code
CarrieHunta at 2007-7-8 8:47:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi CarrieHunt,

thanks for responding. Doesn't ant compile it? I built the webapp, thought that would be enough. I found many posts with similar problems, but nobody could explain to what refers this "cannot be resolved as a type". All the samples of tomcat work, only this one that I've copied and now want to modify doesn't and I don't understand why not. Here's the code:

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.net.URL;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.Source;

import javax.xml.transform.stream.StreamSource;

import javax.xml.transform.stream.StreamResult;

/*

* This sample applies the todo.xsl stylesheet to the

* todo.xml XML document, and returns the transformation

* output (HTML) to the client browser.

*/

public class SimpleXSLTServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException

{

super.init(config);

}

public void doGet (HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

{

// Output goes in the response stream.

PrintWriter out = new PrintWriter (response.getOutputStream());

// The servlet returns HTML.

response.setContentType("text/html");

try

{

TransformerFactory tFactory = TransformerFactory.newInstance();

// Get the XML input document and the stylesheet.

Source xmlSource = new StreamSource(new URL("http://localhost/todo.xml").openStream());

Source xslSource = new StreamSource(new URL("http://localhost/todo.xsl").openStream());

// Generate the transformer.

Transformer transformer = tFactory.newTransformer(xslSource);

// Perform the transformation, sending the output to the response.

transformer.transform(xmlSource, new StreamResult(out));

}

catch (Exception e)

{

out.write(e.getMessage());

e.printStackTrace(out);

}

out.close();

}

}

Thanks a lot!!! :)

javamajaa at 2007-7-8 8:47:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

which sample did you copied? and if you compiled with ant, did it says BUILD SUCCESSFUL at the end?

If problem still continue, I would suggest use Tomcat5 instead of Tomcat6 as it is still beta.

If you are still pretty new, I would suggest you start off a project with the $TOMCAT_HOME/webapps/tomcat-doc/appdev/sample as your starting webapp strutture.

good luck.

thebugslayera at 2007-7-8 8:47:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...