本文实例为大家分享了 Java 异步登录的具体代码 , 供大家参考 , 具体内容如下
1.Loginservletajax.java
package com.scce.servlet; import java.io.ioException; import java.io.printwriter; Importer javax.servlet.servletException; import javax.servlet.http.httpsservlet; import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletReSponse; importer net.sf.json.jsonObject; import com.scce.dao.adminuserdao; import com.scce.pojo.adminuser; La classe publique LoginServletajax étend HttpServlet {@Override Protected Void Service (HttpServletRequest Request, HttpServletResponse Response) lance ServletException, ioException {// todo généré par la méthode de demande.SetcharacteRencoding ("utf-8"); Response.SetContentType ("Text / HTML; charSet = UTF-8"); String Method = request.getMethod (); if (method.equals ("post")) {dologinajax (request, réponse); } else if (method.equals ("get")) {}} public void dologinajax (httpsservletRequest request, httpsservletResponse réponse) lance servlexception, ioException {printwriter out = réponse.getwriter (); // 用户名 String username = request.getParameter ("nom d'utilisateur"); // 密码 String mot de passe = request.getParamètre ("mot de passe"); String msg = ""; AdministrerDao AdministraderDao = new AdministraderDao (); Administrateur administrateur = administrateurdao.QueryUser (nom d'utilisateur, mot de passe); if (administrateur! = null) {msg = "登录成功!"; String JSONOBJ = JSONObject.FromObject (Administrateur) .ToString (); out.print ("{/" msg / ": /" "+ msg +" / ", /" lignes / ":" + jsonobj + "}"); System.out.println ("{/" msg / ": /" "+ msg +" / ", /" lignes / ":" + jsonObj + "}"); } else {msg = "用户名或者密码不正确!"; out.print ("{/" msg / ": /" "+ msg +" / "}"); } out.flush (); out.close (); }} 2.Test2.html
<! Doctype html> <html> <éadf> <tatique> Chapter3-Test2 </Title> <Meta http-equiv = "Keywords" Content = "Keyword1, Keyword2, Keyword3"> <meta http-equiv = "Description" Content = "Il s'agit de ma page"> <méta http-equiv = "Contenu-Type-Type" Content-Type " 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> <! - <! src = "../ js / jQuery-1.8.2.min.js"> </ script> -> <script type = "text / javascript" src = "../ ui / jQuery.min.js"> </ script> <script type = "text / javascrip type = "text / javascript" src = "../ ui / locale / easyui-Lang-zh_cn.js"> </ script> <script type = "text / javascrip boutons: [{text: '登录', iconcls: 'icon-add', handler: function () {console.info ("用户登录!"); ajaxfrm (); // ajax 提交表单的函数}}]}); }); fonction ajaxfrm () {// ------------------------ 注释的是 Ajax 提交方法 ---------------------------- Var LoginList = $ ("# Loginlist"); $ .ajax ({url: '../loginservletajax?tag=test' ,// 相对路径访问 type:' post ', // 提交请求的方式 data: $ (' # form1 '). serialize () +' & noms = liuqin & age = 26 ', // 将表单参数序列化 , 发送到服务器的数据 发送到服务器的数据 (提交额外的参数) Datatype:' json ', // 预期服务器返回的数据类型 -json = {"Msg": "登录成功", "lignes": {"nom d'utilisateur": "admin", ...}} console.info (data); // 调试代码 $ .Messager.Alert ("提示", data.msg); data.Rows.Username + "- 密码 :" + data.Rows.password + "<br/>"; / * var loginlist = $ ("# loginlist"); $ ("# Form1"). Form ("soumettre", {url: "../loginservletajax?tag=test", onSubmit: function (param) {// 提交额外的参数 param.name = "liuqin"; param.age = 27; var username = $ ("# username"). || Mot de passe. $ .Messager.Alert ("提示", data.msg); }); * /} </ script> </ head> <body> <div id = "LoginAdd"> <form id = "form1" metheth = "post"> <s table> <tr> <td> 用户名 : </ td> <td> <entrée id = "username" name = "username"> </ td> </tr> <tr> <td> id = "mot de passe" name = "mot de passe" type = "mot de passe"> </td> </tr> </s table> </ form> </div> <div id = "Loginlist"> 用户信息加载中 ...... </v> <video Controls = "Controls" Src = "../ video / b4934a0c53fc55703bfe3f6843e66166.mp4" type = "vidéo / mp4"> Votre navigateur ne prend pas en charge la balise vidéo. </ video> </ body> </html>以上就是本文的全部内容 , 希望对大家学习 Java 程序设计有所帮助。