简体   繁体   中英

How to make and run servlet with eclipse kepler and apache 7

I am making this new servlet:

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Hello
 */
public class Hello extends HttpServlet
{
    private static final long   serialVersionUID    = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hello()
    {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("It works");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

}

and this is my web.xml in WEB-INF

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstServlet</display-name>
  <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>

   <servlet>
      <servlet-name>Hello</servlet-name>
      <servlet-class>Hello</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>Hello</servlet-name>
      <url-pattern>/hello</url-pattern>
   </servlet-mapping>
</web-app>

display-name and welcome-file-list tags are generated automatically when I created the Dynamic web project. The servlet and servlet-mapping is written by me. When I run the project with appache 7 it says HTTP Status 404 - /FirstServlet/. Where is my mistake?

1) You probably lack all of the welcome files you've listed. A 404 is what should happen when none are present.

2) Your servlet mapping is broken. The servlet-class element requires a fully qualified class name, which would be Servlets.Hello rather than just Hello . When mapped correctly, /FirstServlet/hello will get to your servlet, matching the mapped URI, then finding the registered servlet by name, and finally to the fully qualified class that should be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM