1

Here is the login function.

@RequestMapping("dologin")
@ResponseBody
public boolean dologin(@RequestParam("username") String username,
                       @RequestParam("password")String password, User user, Model model, HttpSession session
, HttpServletRequest request, HttpServletResponse response){
    user = userService.login(user.getUsername(), user.getPassword());
    Boolean flag ;
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    System.out.println("dologin");
    if(userService.login(username,password) != null)
    {
        model.addAttribute("username", username);
        session.setAttribute("user",user);
        flag =  true;
    } else {
        
        flag = false;
    }
    return flag;
}

Here is the login form

<form class="register-form" action="${pageContext.request.contextPath}/register" method="post">
        <input type="text" placeholder="name" name="username" id="name" onkeyup="validate()"/>
        <input type="password" placeholder="password" name="password" id="pw1" /><span id="tips" class="message"></span>
        <input type="password" placeholder="confirm password" id="pw2" onkeyup="validate()"/>
        <button type="submit" id="register" value="register" disabled="disabled">register</button>
        <p class="message">Already registered? <a href="#">login</a></p>
</form>

AJAX:

$(function() {
    $("#loginBtn").click(function() {
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
            type:"POST",
            url:"dologin",
            data:{"username" : username,
                    "password" : password},
            dataType:"json",
            success:function(flag){
                
                if(flag){
                    window.location.href="Dashboard/index.jsp";//This page is target page.
                }else{
                    alert("FAIL");;
                }
            }
        });
    });
});

And when I clicked login button, I didn't get into new page instead page return a string to me like this. enter image description here

I am new beginer, I will appreaciate whether your method works or not, as long as you answer it.

crow Zhong
  • 11
  • 1
  • Hi, where `loginBtn` button , `username` inside your form and is your page refresh ? – Swati Mar 20 '21 at 14:27
  • yeah, I clicked login button and page refresh and return me string, can u help me? – crow Zhong Mar 20 '21 at 14:33
  • You can use `event.preventDefault()` to stop your page refresh i.e : here change this `$("#loginBtn").click(function(event) {` then write `event.preventDefault()` see if that works .For more info check [this](https://stackoverflow.com/questions/9347282/using-jquery-preventing-form-from-submitting) post. – Swati Mar 20 '21 at 15:00
  • w8w8w8, I mean I can't enter the next page, the page returns me a string, its mean something goes wrong. I want to click the button the page brings me to the next page, but seems like now it doesn't work. Just like in login function, I want to return datas to ajax in front end, but something worng in there. – crow Zhong Mar 20 '21 at 15:13
  • I really need your help, it bothers me for couple of days. – crow Zhong Mar 20 '21 at 15:18
  • You are using ajax so page should not get refresh that's the reason i told you to add that in my previous comment . I think your event is not getting just put `alert("in")` inside your `success:function(flag){`.. see if this show up ... – Swati Mar 20 '21 at 15:21
  • no, browser never gives me any alerts when clicked loginbtn. But when I added event.preventDefault(), the windows not get refreshed. OMG, that;s is problem. – crow Zhong Mar 20 '21 at 16:06
  • Does there has one problem is that '$("#loginBtn").click(function(event)', the #loginBtn should be form's name instead btn's name? – crow Zhong Mar 20 '21 at 16:11
  • no it should be button id which is been clicked. – Swati Mar 21 '21 at 07:26
  • wew, I already solved this problem, but there has another problem. Can I get you whatsapp? – crow Zhong Mar 21 '21 at 08:30

0 Answers0