我試圖使用Eclipse和Tomcat 7.0運行一個基本的servlet,但它一直給404錯誤錯誤404的Apache Tomcat使用Eclipse
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
指定的所有步驟,但錯誤依然存在。 請幫忙
您是否將類文件保存在類文件夾中? – SpringLearner
您正在調用/ ServletExample,但您的servlet已映射到/ HelloServlet。順便說一句。該配置不需要.jsp。 –
但url模式是**/HelloServlet **,所以它是正確的提交/ HelloServlet操作。 –