weixin_45289635
落羽轻音
2021-01-20 18:15

小白求助,为什么我点击登陆没有反应

  • java
  • tomcat

这是网页代码:

<
        <div class="login_button">
            <input class="btn" type="button" value="登录" onclick="usesrLogin()">
        </div>

这是js

"use strict";
window.onload = function (){
    background();
    $(".login_input").val("admin001")
    $(".login_tips").css("display","none")
}
function inputFocus(e){
    $(e).parent().siblings(".login_tips").css("color", "#ddd");
}
function inputBlur(e){
    $(e).parent().siblings(".login_tips").css("color", "#aaa");
}
function inputUp(e){
    var count=$(e).val().length;
    if(count==0){
        $(e).parent().siblings(".login_tips").css("display","block");
    }else{
        $(e).parent().siblings(".login_tips").css("display","none");
    }
}
function background(){
    $(".background_img").css("opacity", "1");
}
function usesrLogin() {
    console.info("login")
    $.ajax({
        url: 'UserServlet',
        type: 'POST',
        async: true,
        timeout: 5000,
        data:{
            method: "userLogin",
            userName: $("#username").val(),
            userPassword: $("#password").val()
        },
        beforeSend: function (){

        },
        success: function (msg){
            if (msg == "登录成功"){
                $(location).attr('href', 'main.html')
            }else {
                alert(msg)
            }
        },
        error: function (){

        },
        complete: function (){

        }
    });
}
function usesrRegister(){
    $.ajax({
        url: 'UserServlet',
        type: 'POST',
        async: true,
        timeout: 5000,
        data:{
            method: "userRegister",
            userName: $("#username").val(),
            userPassword: $("#password").val()
        },
        beforeSend: function (){

        },
        success: function (msg){
            alert(msg)
        },
        error: function (){

        },
        complete: function (){

        }
    });
}

这是java代码



@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    private final int MIN_USER_NAME = 8;
    private final int MIN_USER_PASSWORD = 6;
    private static UserService userService = new UserSlmpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        PrintWriter pw = response.getWriter();
        System.out.println("收到的请求method:" + method);
        switch (method){
            case "userRegister":
                userRegister(request, response, pw);
                break;
            case "userLogin":
                userLogin(request, response, pw);
                break;
            default:
        }
    }
    private void userLogin(HttpServletRequest request, HttpServletResponse response, PrintWriter pw) throws IOException, ServletException{
        String user = request.getParameter("userName");
        String pasw = request.getParameter("userPassword");
        if (user.length() < MIN_USER_NAME) {
            pw.write("用户名必须大于8位");
            return;
        }
        if (pasw.length() < MIN_USER_PASSWORD) {
            pw.write("密码必须大于6位");
            return;
        }
        if (userService.ifUserNameNotExist(user)) {
            pw.write("用户名不存在");
            return;
        }
        if (userService.login(user, pasw)) {
            pw.write("登录成功");
        } else {
            pw.write("密码错误");
        }
    }
    private void userRegister(HttpServletRequest request, HttpServletResponse response, PrintWriter pw) throws IOException{
        String user = request.getParameter("userName");
        String pasw = request.getParameter("userPassword");
        if (user.length() < MIN_USER_NAME) {
            pw.write("用户名必须大于8位");
            return;
        }
        if (pasw.length() < MIN_USER_PASSWORD) {
            pw.write("密码必须大于6位");
            return;
        }
        if (userService.register(user, pasw)) {
            pw.write("注册成功");
        } else {
            pw.write("用户名已存在");
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答