我编写了一个应该打印出“Hello World”的servlet,但它无法正常工作。
我在startup.sh
中运行/usr/share/tomcat8/bin/startup.sh
启动tomcat,我的程序位于/usr/share/tomcat8/webapps/hello/WEB-INF/classes/HelloWorld.class
文件夹中。
这是我的web.xml:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
这是我的源代码:
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
public class HelloWorld extends HttpServlet
{
static final long serialVersionUID = 42L;
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("\t<title>Test</title>");
out.println("</head>");
out.println("<body>");
out.println("<b>Hello World!</b>");
out.println("</body>");
out.println("</html>");
}
}
我使用以下命令编译它:
javac -classpath %J2EE_HOME%\lib\j2ee.jar -classpath ../../../../lib/servlet-api.jar HelloWorld.java
没有编译错误,但是当我访问ipaddress:8080/hello/HelloWorld
时,我得到以下错误代码:
exception
java.lang.NullPointerException
org.apache.catalina.loader.WebappClassLoaderBase.binaryNameToPath(WebappClassLoaderBase.java:2562)
org.apache.catalina.loader.WebappClassLoaderBase.findLoadedClass0(WebappClassLoaderBase.java:2730)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1208)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)