代码之家  ›  专栏  ›  技术社区  ›  Nguyễn Mạnh Cường

jsp servlet Etat HTTP 404[重复]

  •  0
  • Nguyễn Mạnh Cường  · 技术社区  · 6 年前

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

    Etat HTTP 404-/IT/UserServlet

    键入融洽关系

    消息/IT/UserServlet

    说明:资源需求是有争议的。

    Apache Tomcat/8.0.27

    这是我的代码: 登记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;
        }
    

    我的网络。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>
    

    当我运行测试寄存器时。jsp和它向我显示了错误,但我不知道在哪里,所以请帮助!

    1 回复  |  直到 6 年前
        1
  •  0
  •   helospark    6 年前

    基于添加的web。xml,您已将URL映射到 /LoginServlet /UserServlet 您正在尝试访问。

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