HTTP状态404 - 请求的资源不可用 -

时间:2015-05-28 08:13:14

标签: java jsp servlets

我遇到过这个问题并花了一些时间在这里搜索类似的问题,但大多数情况都与大/小写错误有关。 问题是,在我家里的电脑上工作得很好,但是现在我已经将它导入我的笔记本电脑上,它就无法正常工作。

当我尝试从jsp文件运行servlet时,我得到HTTP状态404 - /Web/RegisterServlet.do。

我的Jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Dynamic Example</title>
</head>
<body>
    <form action="RegisterServlet.do" method="post">

        <table>
            <tr>
                <td align="right">Username:</td>
                <td align="left"><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td align="right">Real name:</td>
                <td align="left"><input type="text" name="realname" /></td>
            </tr>
            <tr>
                <td align="right">Password:</td>
                <td align="left"><input type="text" name="password1" /></td>
            </tr>
            <tr>
                <td align="right">Password (again):</td>
                <td align="left"><input type="text" name="password2" /></td>
            </tr>
            <tr>
                <td align="right">Email:</td>
                <td align="left"><input type="text" name="email1" /></td>
            </tr>
            <tr>
                <td align="right">Email (again):</td>
                <td align="left"><input type="text" name="email2" /></td>
            </tr>

            <tr>
                <td align="right">Address</td>
                <td align="left"><input type="text" name="address" /></td>
            </tr>
            <tr>
                <td align="right">Country:</td>
                <td align="left"><input type="text" name="country" /></td>
            </tr>
            <tr>
                <td align="right"><input type="submit" value="Register" /></td>
            </tr>

        </table>

    </form>
</body>
</html>

我的servlet:

package web;

import java.io.BufferedWriter;
import java.io.FileWriter;
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;

public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        boolean good = true;
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        String email1 = req.getParameter("email1");
        String email2 = req.getParameter("email2");
        String address = req.getParameter("address");
        String country = req.getParameter("country");

        if (username == "" || realname == "" || password1 == ""
                || password2 == "" || email1 == "" || email2 == ""
                || address == "" || country == "") {
            good = false;
        }

        if (!password1.equals(password2)) {
            good = false;
        }

        if (!email1.equals(email2)) {
            good = false;
        }

        if (good == false) {
            resp.sendRedirect("Register.jsp");
        } else {

            FileWriter fw = new FileWriter("users.txt", true);

            PrintWriter pw = new PrintWriter(fw);
            pw.println("[" + username + "][" + realname + "][" + password1
                    + "][" + email1 + "][" + address + "][" + country + "]");
            pw.close();

            req.getRequestDispatcher("Login.jsp").forward(req, resp);
        }

    }
}

我的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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <servlet>
        <servlet-name>Dynamic Example Servlet</servlet-name>
        <servlet-class>web.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dynamic Example Servlet</servlet-name>
        <url-pattern>/LoginServlet.do</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Dynamic Example Servlets</servlet-name>
        <servlet-class>web.RegisterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dynamic Example Servlets</servlet-name>
        <url-pattern>/RegisterServlet.do</url-pattern>
    </servlet-mapping>
</web-app>

我的文件设置: http://i.stack.imgur.com/U3Qg5.png

2 个答案:

答案 0 :(得分:1)

在RegisterServlet中

req.getRequestDispatcher("WEB-INF/Login.jsp").forward(req, resp);

将位置更改为WEB-INF/Login.jsp

为您的操作添加上下文路径

<form action="${pageContext.request.contextPath}/RegisterServlet.do" method="post">

上下文路径计算资源的完整路径,所以这里 ${pageContext.request.contextPath}将是http://localhost:8080/AppName或您的域名&amp;端口号是。

This是相对路径的绝佳资源。

答案 1 :(得分:-1)

您应该将web.xml文件放在WEB-INF文件夹中。如果部署描述符(web.xml)中有些错误,那么我们只会在Tomcat中获得此错误404。

请检查部署描述符路径。见下面的截图。

enter image description here

我已尝试使用您的代码。我能够成功运行您的代码。我将在结构下面分享结构和结果。

结构:

enter image description here

没有错误的结果消息:

enter image description here