1

我已经为 servlet 编写了这段代码

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


public class Httpservlet1  extends HttpServlet 
{
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException
    {
        String color = request.getParameter("color");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<B>The selected color is: ");
        pw.println(color);
        pw.close();
    }
}

我已经编译了它,它对应的 html 文件动作属性值为

action="http://localhost:8765/HS/HTTPSERVLET">

和 web.xml 包含

servlet-name 四个
servlet-class Httpservlet1

xml 代码格式的servlet-name 四个
url-pattern /HTTPSERVLET 仍然在运行时显示错误消息

在此处输入图像描述

4

1 回答 1

2

也许您的 web.xml 格式不正确。它对我来说没问题。

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <servlet>
    <servlet-name>four</servlet-name>
    <servlet-class>Httpservlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>four</servlet-name>
    <url-pattern>/HTTPSERVLET</url-pattern>
  </servlet-mapping>
</web-app>
于 2012-10-06T09:14:40.510 回答