JavaWeb實現(xiàn)注冊用戶名檢測
本文實例為大家分享了JavaWeb實現(xiàn)注冊用戶名檢測的具體代碼,供大家參考,具體內(nèi)容如下
案例說明
實現(xiàn)一個可以異步獲取用戶名是否被注冊的小案例。如:
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("服務器發(fā)生了錯誤"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? }); ? ? ? ? }); ? ? </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.定義消息的實體類
public class Result { ? ? public static Result NO_REGISTER = new Result(1,"恭喜,可以注冊! "); ? ? public static Result ALREADY_REGISTER = new Result(0, "已經(jīng)被注冊了,請換一個用戶名!"); ? ? 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用
這篇文章主要為大家介紹了Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
本篇文章主要介紹了spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用,具有一定的參考價值,有興趣的可以了解一下。2017-04-04Netty客戶端接入流程NioSocketChannel創(chuàng)建解析
這篇文章主要為大家介紹了Netty客戶端接入流程NioSocketChannel創(chuàng)建源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03詳解Spring Boot中使用@Scheduled創(chuàng)建定時任務
本篇文章中主要介紹了Spring Boot中使用@Scheduled創(chuàng)建定時任務,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03