【发布时间】:2015-12-13 17:16:50
【问题描述】:
我正在使用 tomcat 7 和 jdk 8 开发 ubuntu 服务器 14.04。 我是 java servlet 的新手。所以我把这个教程 http://www.tutorialspoint.com/servlets/servlets-first-example.htm 加红了,并且做了同样的事情(但不是在 ROOT - 在 TESTAPP 中),但是当我尝试 http://localhost:8080/TESTAPP/HelloWorld 时,我得到 404 错误。 目录层次结构:
/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
然后在浏览器中(或使用 curl) 对于http://localhost:8080/TESTAPP 我没问题 对于http://localhost:8080/TESTAPP/HelloWorld,我没有找到。
那么我的错误在哪里? 请帮忙。
【问题讨论】:
标签: java html servlets get request