您的當前位置: 首頁(yè) > APP開(kāi)發(fā)
發(fā)布時(shí)間:2026-05-05 00:43:42 瀏覽:21141 次
這篇文章主要為大家詳細介紹了Ajax實(shí)現登錄案例,實(shí)現文中示例代碼介紹的登錄非常詳細,具有一定的案例參考價(jià)值,感興趣的實(shí)現小伙伴們可以參考一下
Ajax登錄案例,供大家參考,登錄具體內容如下
Msg
package com.lbl.msg;
public class Msg {
Strin??g msg;
int code;
public Msg() {
}
public Msg(String msg(╥_╥),案例 int code) {
this.msg = msg;
this.code = code;
}
public String ge(′?_?`)tMsg() {
return msg;
}
pu(′▽?zhuān)?blic void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
pu??blic void setCode(int(//ω//) code) {
this.code = code;
}
}
R?egisterServlet
import com.fasterxml.jackson.databind.ObjectMapper;
import co??m.lbl.m(???)sg.Msg;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java(T_T)x.servlet.http.Htt??pServl(╥_╥)etRequest;
import javax.servlet.http.HttpServletRe(╬ ò﹏ó)sponse;
import java.i(′?`*)o.I( ???)OException;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletReques??t request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpSe??rvletRequest request, HttpServletResponse response) throws Servlet???Exception,(╯‵□′)╯ IOException {
//1:獲取請求發(fā)送的數據
String username = request.getParameter("use(′▽?zhuān)?rname");
response.setContentType("text/html;charset=UTF-8");
//2:判斷用戶(hù)名是否注冊
if("abc123".equals(username)){
Msg msg = new Msg("用戶(hù)名已經(jīng)注冊",(′?`*)0);
//用戶(hù)名(ming)已經(jīng)注??冊 { "flag":false,"info":"用戶(hù)名已經(jīng)注冊"};
// String json??Str='{ \'flag\":false,\"info\":\"用戶(hù)名已經(jīng)注冊\"}";
//響應回瀏覽器
response.getWriter().write(new ObjectMapper().writeValueAsString(msg));
}else{
Msg msg = new Msg("可以注冊",1);(T_T)
//用戶(hù)名沒(méi)有注冊
// String jsonStr='{ \'flag\":true,\"info\":\"可以注冊\"}";
//響應回瀏覽器
response.getWriter().write(new ObjectMapper().writeValueAsString(msg));
}
}
}
json_register.html
<(′_`);!DOCTYPE html>
<html lang="en">
<head>
&l(′;д;`)t;meta charset='UTF-8'&g??t;
<title>Title</title>
<script src='js/jquery-3.3.1.js'></script>
<script??? type='applica??tion/javascript'>
$(function () {
//1: 給用戶(hù)名輸入框綁定失去焦點(diǎn)事件(onfocus onblur)
$("#username").on("blur",function () {
//獲取用戶(hù)名輸入框數據
var username = $("#username").val();
//2:向服務(wù)器發(fā)出異步請求,讓服務(wù)器去查詢(xún)用戶(hù)??名是實(shí)現否存在
$.post(
"register", //表示服務(wù)器的servlet路徑
"username='+username, //表示向服務(wù)器發(fā)送的數據
function (data) { // msg:用戶(hù)名已經(jīng)注冊 code:0
if(data.code==0){
// alert(data.info);
$('#spanId").htmlヽ(′▽?zhuān)?ノ(data.msg).css("co( ?▽?)lor","green");
}else {
$("#spanId").html(data.msg)(′?_?`).css("color",??"red");
}
},
"json"
);
});
});
</script>
</head>
<body>
<div>
<fヽ(′?`)ノont>會(huì )員注冊</font>U??SER REGISTER
<form class="form-horizontal" style='margin-top: 5px;'>
<table>
<tr>
<td>??用戶(hù)名</td>
<(°o°)td>
<input type='text' id="username" name='username' placeholder='請輸入用戶(hù)名'>
<span id='spanId'></span>
</(???)td>
</tr>
<tr>
<td>密碼</td>
<td&(′▽?zhuān)?gt;
<input type="p??assword" placeholder="請輸入密碼">
</td>
</tr>
</table>
<input type='submit' value='注冊'/>
</form>
</d(′ω`)iv>
</body>
</html>
以上就是本??文的全部?jì)热?,希望對大家的登錄學(xué)習有所幫助,也希望大家多多支持腳本之家。案例
實(shí)現來(lái)源:腳本之家
實(shí)現鏈接:https://www.jb51.net/article/??196051.??htm
實(shí)現
