I have been trying to get a 'Hello World' Servlet application running for two weeks I have tried many examples on the web and now resorted to one found at Safari-books, the only difference is I've removed the package name and its subsequent directory structure in order simplify the problem:
Apache Tomcat 7
By: Aleksa Vukotic; James Goodwill
Publisher: Apress
It just comes down to a 'Hello World' Servlet which fails and is preventing me from doing something more meatier for the interview. I did start out with Eclipse but as you say its cleaner to use the command line which I did when my problems started occurring as I initially thought it was a build issue/deployment conflict, which it probably is but I'm clueless as to what though. I too run the latest tomcat 7 and latest jdk/jre. I build with simply the command line as:
javac HelloWorldServlet.java -cp CATALINA_HOME/lib/servlet-api.jar
I set my CLASSPATH to .;c:\apache-tomcat-7.0.23-windows-x86\apache-tomcat-7.0.23\servlet-api.jar
It builds fine I copy the HelloWorldServlet.java and I copy the HelloWorldServlet.class
My HelloWorldServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
My web.xml:
="1.0"="ISO-8859-1"
<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_3_0.xsd" version="3.0">
<display-name>Chapter 2</display-name>
<description>Apress demo</description>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>
HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/hello.html</url-pattern>
</servlet-mapping>
</web-app>
My Directory structure looks like:
apache-tomcat-7.0.23-windows-x86/
apache-tomcat-7.0.23/
bin/
conf/
lib/
logs/
temp/
webapps/
apress/
jsps/
WEB-INF/
web.xml
Classes/
HelloWorldServlet.class
lib/
work/
I set CATALINA_HOME to c:\apache-tomcat-7.0.23-windows-x86\apache-tomcat-7.0.23
I start tomcat as CATALINA_HOME\bin\catalina start
The logs seem to suggest that the xml files are ok as it displays:
Jan 09, 2012 12:40:51 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23-windows-x86\apache-tomcat-7.0.23\webapps\apress
INFO: Server startup in 1017 ms
In Google chrome I type:
http://localhost:8080/apress/hello.html
I get the following output in my browser:
HTTP Status 500 -HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class HelloWorldServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1815)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)
root cause
java.lang.ClassNotFoundException: HelloWorldServlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1533)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1815)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.
and this is the logs:
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@4ded8e')
Jan 09, 2012 12:40:49 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet helloworld as unavailable
Jan 09, 2012 12:40:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet helloworld
java.lang.ClassNotFoundException: HelloWorldServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
Any help will be greatly appreciated.
Thanks
Alastair