HTTP ERROR 404访问“未找到名称”(Servlet)

时间:2016-04-11 14:13:03

标签: java eclipse servlets

抱歉,我认为这是一个简单的问题,但我无法弄清楚。 (我是Java编程的新手,并尝试制作Web servlet)。我不知道还有什么在这里添加。 Servlet代码来自互联网教程。但是服务器不会显示它。

我的Servlet:

package CarWorld;

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

// Extend HttpServlet class
public class Services_Create extends HttpServlet {

  // Method to handle GET method request.
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>First Name</b>: "
                + request.getParameter("first_name") + "\n" +
                "  <li><b>Last Name</b>: "
                + request.getParameter("last_name") + "\n" +
                "</ul>\n" +
                "</body></html>");
  }
  // Method to handle POST method request.
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }
}

我的web.xml文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <servlet>
        <servlet-name>Services_Create</servlet-name>
        <servlet-class>Services_Create</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>Services_Create</servlet-name>
        <url-pattern>/Services_Create</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

好吧,我认为您的问题出在您的web.xml文件中,您有类似这样的内容:

<url-pattern>/Services_Create</url-pattern>

但它不是模式,Services_Create是你的类,你必须为你的网站编写可用的页面,如* .html,* .php,* .xhtml,你写的错误

/Services_Create

使用&#34; /&#34;你在谈论一个根,在它里面,很多页面,例如

/pages/welcome.xhtml

内部文件夹&#34;页面&#34;你有一个welcome.xhtml文件来显示内容。 因此,您必须将您的值从/ Services_Create更改为您的个人模式,例如* .html或您在项目中使用的任何内容。

希望对你有所帮助。

相关问题