当我在xml中的映射很好时,tomcat会出现404错误

时间:2016-07-29 13:37:35

标签: java xml tomcat

public class Guestbook extends CacheHttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1 L;
    private Vector < GuestbookEntry > entries = new Vector < GuestbookEntry > ();
    private long lastModified = 0; // Time last entry was added
    // Display the current entries, then ask for a new entry
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        printHeader(out);
        printForm(out);
        printMessages(out);
        printFooter(out);
    }
    // Add a new entry, then dispatch back to doGet()
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        handleForm(req, res);
        doGet(req, res);
    }
    private void printHeader(PrintWriter out) throws ServletException {
        out.println("<HTML><HEAD><TITLE>Guestbook</TITLE></HEAD>");
        out.println("<BODY>");
    }
    private void printForm(PrintWriter out) throws ServletException {
        out.println("<FORM METHOD=POST action='/hello.html'>"); // posts to itself
        out.println("<B>Please submit your feedback:</B><BR>");
        out.println("Your name: <INPUT TYPE=TEXT NAME=name><BR>");
        out.println("Your email: <INPUT TYPE=TEXT NAME=email><BR>");
        out.println("Comment: <INPUT TYPE=TEXT SIZE=50 NAME=comment><BR>");
        out.println("<INPUT TYPE=SUBMIT VALUE=\"Send Feedback\"><BR>");
        out.println("</FORM>");
        out.println("<HR>");
    }
    private void printMessages(PrintWriter out) throws ServletException {
        String name, email, comment;
        Enumeration < GuestbookEntry > e = entries.elements();
        while (e.hasMoreElements()) {
            GuestbookEntry entry = (GuestbookEntry) e.nextElement();
            name = entry.name;
            if (name == null) {
                name = "Unknown user";
                email = "Unknown email";
            }
            email = entry.email;
            comment = entry.comment;
            if (comment == null) comment = "No comment";
            out.println("<DL>");
            out.println("<DT><B>" + name + "</B> (" + email + ") says");
            out.println("<DD><PRE>" + comment + "</PRE>");
            out.println("</DL>");
            // Sleep for half a second to simulate a slow data source
            try {
                Thread.sleep(500);
            } catch (InterruptedException ignored) {}
        }
    }
    private void printFooter(PrintWriter out) throws ServletException {
        out.println("</BODY>");
        out.println("</HTML>");
    }
    private void handleForm(HttpServletRequest req,
        HttpServletResponse res) {
        GuestbookEntry entry = new GuestbookEntry();
        entry.name = req.getParameter("name");
        entry.email = req.getParameter("email");
        entry.comment = req.getParameter("comment");
        entries.addElement(entry);
        // Make note we have a new last modified time
        lastModified = System.currentTimeMillis();
    }
    public long getLastModified(HttpServletRequest req) {
        return lastModified;
    }
}
class GuestbookEntry {
    public String name;
    public String email;
    public String comment;
}

在我使用的XML文件中

<web-app>
<servlet>
<servlet-name>
GuestBook
</servlet-name>
<servlet-class>
Guestbook
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
GuestBook
</servlet-name>
<url-pattern>
/hello.html
</url-pattern>
</servlet-mapping>
</web-app>

我使用的一切都很好,但tomcat仍然给我404错误。虽然我试过不同的方法,但它仍然给我一个错误。 如果有人会提供解决方案,那么我们将非常感激。

提前致谢

1 个答案:

答案 0 :(得分:0)

我们需要创建一个单独的html页面,并在代码中编写与“PrintForm”方法相同的内容。如果我们这样做,那么这个servlet将完美地工作。虽然这个servlet用于服务器缓存,但我希望它能在将来帮助你。

谢谢