简体   繁体   English

使用Eclipse的Apache Tomcat中的错误404

[英]Error 404 in Apache Tomcat using Eclipse

I'm trying to run a basic servlet using Eclipse and Tomcat 7.0 but it keeps giving the 404 error 我正在尝试使用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

The different codes are : Home.jsp 不同的代码是: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 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 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>

I've followed all the steps specified in this thread 我已经遵循了该线程中指定的所有步骤

Yet the error persists. 但是错误仍然存​​在。 Please help 请帮忙

You have a URL pattern /HelloServlet in your web.xml . 您的web.xml有一个URL模式/HelloServlet So you should try to connect 127.0.0.1:8080/HelloServlet , not 127.0.0.1:8080/ServletExample/ . 因此,您应该尝试连接127.0.0.1:8080/HelloServlet ,而不是127.0.0.1:8080/ServletExample/

I think you are accesing /ServletExample/ and this is your context. 我认为您正在访问/ ServletExample / ,这就是您的上下文。 Put in your web.xml : 放入您的web.xml:

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

Hope this helps. 希望这可以帮助。

像这样在web.xml添加包名称。

<servlet-class>package.HelloWorld</servlet-class> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM