找不到java servlet 404

时间:2017-12-25 12:10:31

标签: servlets http-status-code-404

servlet是

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletWriteHtml extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException
{
    PrintWriter out = response.getWriter();
    java.util.Date today = new java.util.Date();
    out.println("html" +
                "<body>" +
                "<h1 align = center>Write Html<h1>"
                + "<br>" + today + "<body>" + "</html>");                        
        }
}

web.xml是

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="file-upload" 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>And</servlet-name>
        <servlet-class>ServletWriteHtml</servlet-class>
    </servlet>  
    <servlet-mapping>
        <servlet-name>ServletWriteHtml</servlet-name>
        <url-pattern>/Alfa</url-pattern>
    </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

网址为http://localhost:8080/JavaWeb/Alfa 编译了servlet,类在WEB-INF文件夹中

HTTP状态404 - 未找到 类型状态报告 未找到信息 description请求的资源不可用。 GlassFish Server开源版4.1.1

1 个答案:

答案 0 :(得分:1)

servlet和servlet-mapping中的servlet-name应该相同

你需要在servlet-class

中提供完整的课程(包括课程)

例如:

<servlet>
    <servlet-name>ServletWriteHtml</servlet-name>
    <servlet-class>my.packages.ServletWriteHtml</servlet-class>
</servlet>