博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java+serlvet+ajax+session实现登录注销
阅读量:3945 次
发布时间:2019-05-24

本文共 2274 字,大约阅读时间需要 7 分钟。

登录html

Login

登录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());

转载地址:http://cxowi.baihongyu.com/

你可能感兴趣的文章
Android术语列表概览
查看>>
全方位解读Android多媒体框架源码
查看>>
Android音乐编程的管理音频硬件
查看>>
Android UI控件组合应用之一:建立数据模型
查看>>
避免Andriod平台图片失真的图片形式
查看>>
Android之Gridview图片列表
查看>>
objdump的使用方法
查看>>
编译错误处理noproguard.classes-with-local.dex已杀死
查看>>
LTE - CSFB技术
查看>>
GSM链路层信令协议
查看>>
技术道德
查看>>
“需求为王”才是根本
查看>>
高效率的危害
查看>>
寻找边缘性创新
查看>>
让创意瞄准市场
查看>>
高效经理人应具有的八个重要习惯
查看>>
优秀的领导者能读懂人才
查看>>
大智若愚也是领导力
查看>>
android如何编译MTK的模拟器
查看>>
android如何添加AP中要使用的第三方JAR文件
查看>>