java - 使用 Eclipse 在 Apache Tomcat 中出现错误 404

标签 java eclipse apache jsp tomcat

我正在尝试使用 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

不同的代码是: 首页.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>

Hello World .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>" ); 
    } 
}

网络.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 中指定的所有步骤进行操作

但错误仍然存​​在。 请帮忙

最佳答案

我认为您正在访问 /ServletExample/ 并且这是您的上下文。放入您的 web.xml :

<welcome-file-list>
    <welcome-file>Home.jsp</welcome-file>
</welcome-file-list> 

希望这对您有所帮助。

关于java - 使用 Eclipse 在 Apache Tomcat 中出现错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21571582/

相关文章:

php - 什么是 HTTP 严格传输安全 (HSTS) (Apache)?

java - Apache HttpClient ResponseHandler 异常

java - Webjars 定位器不适用于基于 XML 的 Spring MVC 4.2.x 配置?

java - "Gradle Dependencies"无法在 'Apache Tomcat' 运行配置中正确显示

eclipse - 将 J2EE 安装到现有的 Eclipse IDE 中

java - 如何修改此代码以使其在 Android API 3.0 上运行?

PHP session 损坏

java - 正则表达式模式中的 ANT 属性

java - 非法文本 block 打开定界符序列,缺少行终止符

java - Android DatagramSocket 无法正常工作