Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 为什么程序显示404错误。。单击时显示HTML页面_Servlets_Sample - Fatal编程技术网

Servlets 为什么程序显示404错误。。单击时显示HTML页面

Servlets 为什么程序显示404错误。。单击时显示HTML页面,servlets,sample,Servlets,Sample,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

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">
    <display-name>FirstProject</display-name>

    <servlet>
    <servlet-name>ServletClass1</servlet-name>
    <servlet-class>com.test.ServletClass1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletClass1</servlet-name>
        <url-pattern>/first.*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>
您的URL需要与servlet映射的名称匹配。现在没有。修复servlet映射,如下所示:

<url-pattern>/first</url-pattern>
另见: -包含一些Hello World示例
与具体问题无关,请确保您没有阅读严重过时的资源/书籍/教程。您的web.xml被声明为符合Servlet 2.4,它已经有将近9年的历史了。我们已经在Servlet 3.0上使用了2.5年。

当您手动发送响应时,应该调用out.flush来提交响应。另外,当像这样手动输出响应时,您将永远无法到达first.html页面我将响应保留在html标记中..现在为什么程序不执行?但是您有一个servlet和一个物理文件,映射到first.html,即使它工作,这没有任何意义。如果您的web上下文根使用正确,并且您浏览first.html,这些设置应该调用您的ServletClass1.doGet方法,但它从不使用flush提交响应,因此不会向服务器发送任何内容@jb10210 am能够访问first.html。。在提交表格后,它会给出一个404错误,感谢所有回复。程序确实执行了。
package com.test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletClass1 extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out= response.getWriter();  
        String msg = request.getParameter("t");
        out.println("the msg is"+msg);  
    }
}
<url-pattern>/first</url-pattern>