这可能是一个非常小的问题,但我仍然无法找到解决方案。 我正在使用Apace Tomcat 7.0.54和JAVA 1.7
我在创建html页面的网页中收到以下错误,并在web.xml中添加了servlet名称和URL映射
Error message:
HTTP Status 404 - /sayhello/
type Status report
message /sayhello/
description The requested resource is not available.
以下是我的JAVA代码。
// To save as "<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.java"
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the response MIME type of the response message
response.setContentType("text/html");
// Allocate a output writer to write the response message into the network socket
PrintWriter out = response.getWriter();
// Write the response message, in an HTML page
try {
out.println("<html>");
out.println("<head><title>Hello, World</title></head>");
out.println("<body>");
out.println("<h1>Hello, world!</h1>"); // says Hello
// Echo client's request information
out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
out.println("<p>Protocol: " + request.getProtocol() + "</p>");
out.println("<p>PathInfo: " + request.getPathInfo() + "</p>");
out.println("<p>Remote Address: " + request.getRemoteAddr() + "</p>");
// Generate a random number upon each request
out.println("<p>A Random Number: <strong>" + Math.random() + "</strong></p>");
out.println("</body></html>");
} finally {
out.close(); // Always close the output writer
}
}
}
Web.xml中
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0"
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">
<!-- To save as "hello\WEB-INF\web.xml" -->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<!-- Note: All <servlet> elements MUST be grouped together and
placed IN FRONT of the <servlet-mapping> elements -->
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/sayhello</url-pattern>
</servlet-mapping>
</web-app>
这里应该纠正什么?还有一件事,我也试过执行另一个JAVA数据库servlet。它也没有返回任何内容并给了我同样的错误。
答案 0 :(得分:1)
正如您所说,您将网址设为localhost:8080/sayhello
试试这个网址http://localhost:8080/projectName/sayhello
答案 1 :(得分:1)
您在网址中缺少项目名称/上下文路径:
http://localhost:8080/ProjectName/sayhello
答案 2 :(得分:0)
您的
<Servlet-class>
值是否属于任何包裹?如果是, 然后给出“PackageName.ServletClassName”。如果不是尝试的话 以http://localhost:8080/ProjectName/sayhello
开头。如果是 不是问题尝试更改端口(8080)(右键单击 server-&gt; properties-&gt;切换位置。)。在服务器目录下 单击属性并在服务器位置下尝试更改为“使用 tomcat安装“。尽量保持index.html不仅在web-inf中而且还在 在web内容文件夹中也是(如果你有index.html文件)。