本文实例为大家分享了java异步登录的具体代码,供大家参考,具体内容如下
1.LoginServletAjax.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.scce.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import com.scce.dao.AdminUserDao; import com.scce.pojo.AdminUser; public class LoginServletAjax extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding( "utf-8" ); response.setContentType( "text/html;charset=utf-8" ); String method = request.getMethod(); if (method.equals( "POST" )) { doLoginAjax(request, response); } else if (method.equals( "GET" )) { } } public void doLoginAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 用户名 String username = request.getParameter( "username" ); // 密码 String password = request.getParameter( "password" ); String msg = "" ; AdminUserDao adminUserDao = new AdminUserDao(); AdminUser adminUser = adminUserDao.queryUser(username, password); if (adminUser != null ) { msg= "登录成功!" ; String jsonObj= JSONObject.fromObject(adminUser).toString(); out.print( "{\"Msg\":\"" +msg+ "\",\"rows\":" +jsonObj+ "}" ); System.out.println( "{\"Msg\":\"" +msg+ "\",\"rows\":" +jsonObj+ "}" ); } else { msg= "用户名或者密码不正确!" ; out.print( "{\"Msg\":\"" +msg+ "\"}" ); } out.flush(); out.close(); } } |
2.test2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<!DOCTYPE html> < html > < head > < title >chapter3-test2</ title > < meta http-equiv = "keywords" content = "keyword1,keyword2,keyword3" > < meta http-equiv = "description" content = "this is my page" > < meta http-equiv = "content-type" content = "text/html; charset=UTF-8" > < link rel = "stylesheet" href = "../UI/themes/icon.css" type = "text/css" ></ link > < link rel = "stylesheet" href = "../UI/themes/default/easyui.css" type = "text/css" ></ link > <!-- <script type="text/javascript" src="../js/jquery-1.8.2.min.js"></script> --> < script type = "text/javascript" src = "../UI/jquery.min.js" ></ script > < script type = "text/javascript" src = "../UI/jquery.easyui.min.js" ></ script > < script type = "text/javascript" src = "../UI/locale/easyui-lang-zh_CN.js" ></ script > < script type = "text/javascript" > //string number boolean null object undefined function $(function() { $("#LoginAdd").dialog({ title : "用户登录", collapsible : 'true', width : 300, height : 200, buttons : [ { text : '登录', iconCls : 'icon-add', handler : function() { console.info("用户登录!"); ajaxFrm();//ajax提交表单的函数 } } ] }); }); function ajaxFrm() { //------------------------注释的是ajax提交方法---------------------------- var LoginList = $("#LoginList"); $.ajax({ url : '../LoginServletAjax?tag=test',//相对路径访问 type : 'POST', //提交请求的方式 data : $('#form1').serialize()+'&names=liuqin&age=26',//将表单参数序列化,发送到服务器的数据(提交额外的参数) dataType : 'json', //预期服务器返回的数据类型-json object success : function(data) {//请求成功后将调用此方法var data = {"Msg":"登录成功","rows":{"username":"admin",...}} console.info(data);//调试代码 $.messager.alert("提示", data.Msg); LoginList.html("");//清空数据 if (data.rows) { var stra = LoginList.html() + "用户名:" + data.rows.username + "--密码:" + data.rows.password + "< br />"; LoginList.html(stra); } }, error : function(error) { //请求失败时将调用此方法 console.info(error); } }); /*var LoginList = $("#LoginList"); $("#form1").form("submit", { url: "../LoginServletAjax?tag=test", onSubmit: function (param) { //提交额外的参数 param.name="liuqin"; param.age=27; var username = $("#username").val(); var password = $("#password").val(); if (username.length == 0 || password.length == 0) { $.messager.alert('警告', '请输入用户名和密码'); return false; } return true; }, success: function (data) { //var data = eval('(' + data + ')'); var data = $.parseJSON(data);//服务器端返回json字符串转成json对象-js object console.info(data); $.messager.alert("提示", data.Msg); LoginList.html("");//清空数据 if (data.rows) { var stra = LoginList.html() + "用户名:" + data.rows.username + "--密码:" + data.rows.password + "< br />"; LoginList.html(stra); } } });*/ } </ script > </ head > < body > < div id = "LoginAdd" > < form id = "form1" method = "post" > < table style = "width: 100%;" > < tr > < td > 用户名: </ td > < td > < input id = "username" name = "username" class = "easyui-validatebox textbox" > </ td > </ tr > < tr > < td > 密码: </ td > < td > < input id = "password" name = "password" class = "easyui-validatebox textbox" type = "password" > </ td > </ tr > </ table > </ form > </ div > < div id = "LoginList" > 用户信息加载中...... </ div > < video width = "320" height = "240" controls = "controls" src = "../video/B4934A0C53FC55703BFE3F6843E66166.mp4" type = "video/mp4" > Your browser does not support the video tag. </ video > </ body > </ html > |
以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。