java異步方法完成登錄。本站提示廣大學習愛好者:(java異步方法完成登錄)文章只能為提供參考,不一定能成為您想要的結果。以下是java異步方法完成登錄正文
本文實例為年夜家分享了java異步登錄的詳細代碼,供年夜家參考,詳細內容以下
1.LoginServletAjax.java
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
<!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 > <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法式設計有所贊助。