我正在使用tomcat 7和jdk 8在Ubuntu 14.04上工作。 我是新来的java servlets。所以我冲这个教程http://www.tutorialspoint.com/servlets/servlets-first-example.htm,并做了同样的事情(但不是在根 - 在TESTAPP),但是当我尝试http://localhost:8080/TESTAPP/HelloWorld我得到404错误。 目录层次结构:Tomcat没有看到我的servlet
/var/lib/tomcat7/webapps/TESTAPP/
|
-- index.html
-- META_INF/
-- WEB_INF/
|
----- web.xml
----- classes/
|
-------- HelloWorld.java
-------- HelloWorld.class
的index.html:
<h1>TESTAPP</h1>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="TESTAPP"
version="3.0">
<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>
</web-app>
HelloWorld.java:
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// do nothing.
}
}
然后我做了
export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar
javac HelloWorld.java
sudo service tomcat7 restart
catalina.out的:http://pastebin.com/raw.php?i=5SM3vatg
所以后来在browseer(或卷曲) 为http://localhost:8080/TESTAPP我得到OK 为http://localhost:8080/TESTAPP/HelloWorld我得到找不到。
那么我的错误在哪里? 请帮忙。
你说得对。我写这篇文章时犯了错误。我编辑了我的帖子。 – GALIAF95
你可以发布Tomcat日志的相关部分吗?它应该在/var/lib/tomcat7/logs/catalina.out你的情况 –
我不知道什么是相关的部分,但我删除了catalina.out并重新启动tomcat7,所以这个文件再次出现。我添加文件到帖子的末尾。 – GALIAF95