显示HTTP状态404的Servlet程序

时间:2012-10-06 08:04:22

标签: java servlets tomcat7

我已经为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

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

enter image description here

1 个答案:

答案 0 :(得分: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>