文章詳情頁
Ajax實現(xiàn)登錄案例
瀏覽:32日期:2022-06-11 16:38:40
Ajax登錄案例,供大家參考,具體內(nèi)容如下
Msg
package com.lbl.msg;public class Msg { String msg; int code; public Msg() { } public Msg(String msg, int code) { this.msg = msg; this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; }}
RegisterServlet
package com.lbl.servlet;import com.fasterxml.jackson.databind.ObjectMapper;import com.lbl.msg.Msg;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet("/register")public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1:獲取請求發(fā)送的數(shù)據(jù) String username = request.getParameter("username"); response.setContentType("text/html;charset=UTF-8"); //2:判斷用戶名是否注冊 if("abc123".equals(username)){ Msg msg = new Msg("用戶名已經(jīng)注冊",0); //用戶名已經(jīng)注冊 {"flag":false,"info":"用戶名已經(jīng)注冊"};// String jsonStr = "{\"flag\":false,\"info\":\"用戶名已經(jīng)注冊\"}"; //響應(yīng)回瀏覽器 response.getWriter().write(new ObjectMapper().writeValueAsString(msg)); }else{ Msg msg = new Msg("可以注冊",1); //用戶名沒有注冊// String jsonStr = "{\"flag\":true,\"info\":\"可以注冊\"}"; //響應(yīng)回瀏覽器 response.getWriter().write(new ObjectMapper().writeValueAsString(msg)); } }}
json_register.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery-3.3.1.js"></script> <script type="application/javascript"> $(function () { //1: 給用戶名輸入框綁定失去焦點事件(onfocus onblur) $("#username").on("blur",function () {//獲取用戶名輸入框數(shù)據(jù)var username = $("#username").val();//2:向服務(wù)器發(fā)出異步請求,讓服務(wù)器去查詢用戶名是否存在$.post( "register", //表示服務(wù)器的servlet路徑 "username="+username, //表示向服務(wù)器發(fā)送的數(shù)據(jù) function (data) { // msg:用戶名已經(jīng)注冊 code:0 if(data.code==0){ // alert(data.info); $("#spanId").html(data.msg).css("color","green"); }else { $("#spanId").html(data.msg).css("color","red"); } }, "json"); }); }); </script></head><body><div> <font>會員注冊</font>USER REGISTER <form> <table> <tr><td>用戶名</td><td> <input type="text" id="username" name="username" placeholder="請輸入用戶名"> <span id="spanId"></span></td> </tr> <tr><td>密碼</td><td> <input type="password" placeholder="請輸入密碼"></td> </tr> </table> <input type="submit" value="注冊"/> </form></div></body></html>
運行效果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持。
標簽:
Ajax
相關(guān)文章:
1. Ajax實現(xiàn)表格中信息不刷新頁面進行更新數(shù)據(jù)2. laravel ajax curd 搜索登錄判斷功能的實現(xiàn)3. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)4. 解決ajax的delete、put方法接收不到參數(shù)的問題方法5. 詳談ajax返回數(shù)據(jù)成功 卻進入error的方法6. axios和ajax的區(qū)別點總結(jié)7. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題8. 爬取今日頭條Ajax請求9. 利用ajax+php實現(xiàn)商品價格計算10. jsp實現(xiàn)簡單用戶7天內(nèi)免登錄
排行榜
