java+serlvet+ajax+session实现登录注销

登录html

<form method="post" class="form-validate">
                    <div class="form-group">
                      <input id="login-username" type="text" name="loginUsername" required data-msg="Please enter your username" class="input-material">
                      <label for="login-username" class="label-material">User Name</label>
                    </div>
                    <div class="form-group">
                      <input id="login-password" type="password" name="loginPassword" required data-msg="Please enter your password" class="input-material">
                      <label for="login-password" class="label-material">Password</label>
                    </div><a id="login"  class="btn btn-primary" onclick="login()">Login</a>
                    <!-- This should be submit button but I replaced it with <a> for demo purposes-->
                  </form>

登录js

function login() {
    var username = document.getElementById("login-username").value //获取文本框的内容;
    var password = document.getElementById("login-password").value

    username = username.toString()
    password = password.toString()
	console.log(password)

    $.ajax({
        type:"POST",
        url:"Login",
        data:{
            username:username,
            password:password,
        },
        dataType:"json",
        success:function(result){
            if ( result.state == 200)
            {
                self.location='index.html'
            }
            else alert(result.error)

        },
        error:function(result){
            alert('服务器连接错误')
        }
    })
}

java serlvet

        response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String uname =  request.getParameter("username");
        String pwd = request.getParameter("password");
        JSONObject jsonObject = new JSONObject();
        if (uname.equals("admin")&&pwd.equals("admin")){
        	 //创建session对象
            HttpSession session = request.getSession();
            //把用户数据保存在session域对象中
            session.setAttribute("uname", uname);
        	jsonObject.put("state", "200");
            System.out.println(jsonObject.toString());
        	out.print(jsonObject.toString());
        }
        else{
        	System.out.println(uname);
        	jsonObject.put("state", "404");
            jsonObject.put("error", "请输入正确的账号和密码!");
            System.out.println(jsonObject.toString());
        	out.print(jsonObject.toString());
        }

注销serlvet

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
        JSONObject jsonObject = new JSONObject();
        HttpSession session = request.getSession(false);//防止创建Session
        session.invalidate();
    	jsonObject.put("state", "200");
        jsonObject.put("error", "注销成功!");
        System.out.println(jsonObject.toString());
    	out.print(jsonObject.toString());   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值