我正在尝试使用Eclipse和Tomcat 7.0运行一个基本的servlet,但它一直给出404错误
HTTP Status 404 - /ServletExample/
--------------------------------------------------------------------------------
type Status report
message /ServletExample/
description The requested resource (/ServletExample/) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.27
不同的代码是: 回到Home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My first JSP</title>
</head>
<body>
<form action="HelloServlet">
Please enter a colour<br>
<input type="text" name="color" size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>
HelloWorld.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class HelloWorld extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// reading the user input
String color= request.getParameter("color");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" " +
"\"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" +
"<title> My first jsp </title> \n" +
"</head> \n" +
"<body> \n" +
"<font size=\"12px\" color=\"" + color +"\">" +
"Hello World" +
"</font> \n" +
"</body> \n" +
"</html>" );
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
我已按照this thread
中指定的所有步骤操作但错误仍然存在。 请帮忙
答案 0 :(得分:0)
您的/HelloServlet
中有一个网址格式web.xml
。因此,您应该尝试连接127.0.0.1:8080/HelloServlet
,而不是127.0.0.1:8080/ServletExample/
。
答案 1 :(得分:0)
我认为您正在访问 / ServletExample / ,这是您的上下文。放入你的web.xml:
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
希望这有帮助。
答案 2 :(得分:0)
在web.xml
中添加包名称。
<servlet-class>package.HelloWorld</servlet-class>