jsp servlet Etat HTTP 404 [重复]

Posted

技术标签:

【中文标题】jsp servlet Etat HTTP 404 [重复]【英文标题】:jsp servlet Etat HTTP 404 [duplicate] 【发布时间】:2018-05-16 14:59:12 【问题描述】:

我在编程时遇到了这个问题:

Etat HTTP 404 - /IT/UserServlet

键入报告d''état

消息 /IT/UserServlet

description La ressource demandée n''est pas disponible.

Apache Tomcat/8.0.27

这是我的代码: register.jsp

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Register Page</title>
        <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
        <link rel="stylesheet" href="css/register_style.css">

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () 
                var x_timer;
                $("#username").keyup(function (e) 
                    clearTimeout(x_timer);
                    var user_name = $(this).val();
                    x_timer = setTimeout(function () 
                        check_username_ajax(user_name);
                    , 1000);
                );

                function check_username_ajax(username) 
                    $("#user-result").html('<img src="img/ajax-loader.gif" />');
                    $.post('CheckUserNameServlet', 'username': username, function (data) 
                        $("#user-result").html(data);
                    );
                
            );
        </script>
    </head>
    <body>
        <form action="UserServlet" method="POST">
            <h1>Sign up</h1><br/>
            <span class="input"></span>
            <input type="text" name="name" id = "name" placeholder="Full name" autofocus autocomplete="off" />
            <span class="input" ></span>
            <input type="text" name="username" id="username" placeholder="username" />
            <span id = "user-result"></span>
            <span id="passwordMeter"></span>
            <input type="password" name="password" id="password" placeholder="Password" title="Password min 8 characters. At least one UPPERCASE and one lowercase letter" required pattern="(?=^.8,$)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"/>
            <input type="hidden" value="insert" name="command">
            <button type="submit" value="Sign Up" title="Submit form" class="icon-arrow-right"><span>Sign up</span></button>
        </form>                
    </body>

这是 sevlet:

public class UserServlet extends HttpServlet 
    UserDAO userDao = new UserDAO();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
        //processRequest(request, response);
        String command = request.getParameter("command");
        String url="";
        switch(command)
            case"insert":
                User user = new User();
                user.setUserId(new Date().getTime());
                user.setName(request.getParameter("name"));
                user.setUserName(request.getParameter("username"));
                user.setPassword(request.getParameter("password"));
                user.setCreateTime(Date.from(Instant.now()));
                user.setUpdateTime(Date.from(Instant.now()));
                userDao.InsertUser(user);
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                url = "index.jsp";
                break;               

        
        RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
        rd.forward(request, response);
    

这是 UseDAO:

public boolean InsertUser(User u) 
        Connection conn = Connect.getConnecttion();
        String insert_user = "INSERT INTO user(name, user_name, password, role, create_time, update_time) values(?,?,?,?,?,?)";
        try 
            PreparedStatement ps = conn.prepareStatement(insert_user);
            ps.setString(1, u.getName());
            ps.setString(2, u.getUserName());
            ps.setString(3, u.getPassword());
            ps.setInt(4, 4);
            ps.setDate(5, Date.valueOf(LocalDate.now()));
            ps.setDate(6, Date.valueOf(LocalDate.now()));
            ps.executeUpdate();
            return true;
         catch (SQLException ex) 
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        
        return false;
    

我的 web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>Controller.UserServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>CheckUserNameServlet</servlet-name>
        <servlet-class>Controller.CheckUserNameServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CheckUserNameServlet</servlet-name>
        <url-pattern>/CheckUserNameServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

</web-app>

当我运行测试 register.jsp 并显示错误但我不知道在哪里,所以请帮忙!

【问题讨论】:

404 表示未找到,因此很可能是 URL 配置的问题。附加 web.xml 文件,因为在那里配置了 URI。 【参考方案1】:

根据添加的 web.xml,您已将 URL 映射到 /LoginServlet,而不是您尝试访问的 /UserServlet

还要确保上下文路径 (/IT) 在您的服务器中是正确的。

【讨论】:

哦,我可以通过创建新的 servlet 并将 userservlet 粘贴到新的 servlet 来运行它 @NguyễnMạnhCường 那么解决了吗? 是的,我从您的回答中意识到了这一点,感谢您的帮助

以上是关于jsp servlet Etat HTTP 404 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”