JavaWeb實(shí)現(xiàn)注冊用戶名檢測
本文實(shí)例為大家分享了JavaWeb實(shí)現(xiàn)注冊用戶名檢測的具體代碼,供大家參考,具體內(nèi)容如下
案例說明
實(shí)現(xiàn)一個(gè)可以異步獲取用戶名是否被注冊的小案例。如:


1.編寫Html與js:
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Login</title>
? ? <script src="/jqueryWeb/js/jquery-3.3.1.js"></script>
? ? <script>
? ? ? ? $(function () {
? ? ? ? ? ? $("#username").on("blur",function () {
? ? ? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? ? ? url : "/jqueryWeb/checkUsername",
? ? ? ? ? ? ? ? ? ? data : "username="+$("#username").val(),
? ? ? ? ? ? ? ? ? ? dataType : "json" ,
? ? ? ? ? ? ? ? ? ? type : "post",
? ? ? ? ? ? ? ? ? ? success : function (data) {
? ? ? ? ? ? ? ? ? ? ? ? if(data.code == 1){
? ? ? ? ? ? ? ? ? ? ? ? ? ? $("#msg").css("color","green");
? ? ? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? $("#msg").css("color","red");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? $("#msg").css("display","inline")
? ? ? ? ? ? ? ? ? ? ? ? $("#msg").text(data.msg);
? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? error: function () {
? ? ? ? ? ? ? ? ? ? ? ? alert("服務(wù)器發(fā)生了錯(cuò)誤");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? });
? ? ? ? });
? ? </script>
</head>
<body>
? ? <form action="#" method="post">
? ? ? ? <input id="username" name="username" type="text" placeholder="注冊用戶名"/><br>
? ? ? ? <label id="msg" style="display: none"></label><br>
? ? ? ? <input id="paw" name="paw" type="password" placeholder="密碼"><br>
? ? ? ? <br>
? ? ? ? <input type="submit" value="提交"/>
? ? </form>
</body>
</html>2.定義消息的實(shí)體類
public class Result {
? ? public static Result NO_REGISTER = new Result(1,"恭喜,可以注冊! ");
? ? public static Result ALREADY_REGISTER = new Result(0, "已經(jīng)被注冊了,請換一個(gè)用戶名!");
? ? private int Code;
? ? private String msg;
? ? public Result() {
? ? }
? ? public Result(int code, String msg) {
? ? ? ? Code = code;
? ? ? ? this.msg = msg;
? ? }
? ? //get,set方法
?)3.編寫Servlet
@WebServlet("/checkUsername")
public class LoginController extends javax.servlet.http.HttpServlet {
? ? private List<String> list;
? ? @Override
? ? public void init(ServletConfig config) throws ServletException {
? ? //模擬已經(jīng)被注冊的用戶名
? ? ? ? list = new ArrayList<String>();
? ? ? ? list.add("zhangsan");
? ? ? ? list.add("lisi");
? ? ? ? list.add("wangwu");
? ? ? ? list.add("zhaoliu");
? ? }
? ? protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
? ? ? ? String username = request.getParameter("username");
? ? ? ? Result result = null;
? ? ? ? if(list.contains(username)){
? ? ? ? ? ? result = Result.ALREADY_REGISTER;
? ? ? ? }else{
? ? ? ? ? ? result = Result.NO_REGISTER;
? ? ? ? }
? ? ? ? response.setContentType("text/html;charset=utf-8");
? ? ? ? response.getWriter().println(new ObjectMapper().writeValueAsString(result));
? ? }
? ? protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
? ? ? ? doPost(request,response);
? ? }
}效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫的更新批處理方式
這篇文章主要介紹了springboot整合mybatis實(shí)現(xiàn)數(shù)據(jù)庫的更新批處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot解決循環(huán)調(diào)用問題
作者在將SpringBoot從1.5版本升級至2.6版本,并遷移至阿里云上運(yùn)行后,遇到了循環(huán)調(diào)用問題,在Jetty容器中運(yùn)行沒問題,但在Tomcat容器中就出現(xiàn)了循環(huán)引用問題,原因是SpringBoot 2.6不鼓勵(lì)循環(huán)引用,暴露出該問題,作者提供了兩種解決思路2024-10-10
Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)
這篇文章主要介紹了Java IO學(xué)習(xí)之緩沖輸入流(BufferedInputStream)的相關(guān)資料,需要的朋友可以參考下2017-02-02
spring boot整合redis主從sentinel方式
這篇文章主要介紹了spring boot整合redis主從sentinel方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型
面向?qū)ο缶幊?Java中的抽象數(shù)據(jù)類型...2006-12-12

