JQuery用戶名校驗的具體實現(xiàn)
本實例為大家分享了JQuery用戶名校驗功能,分享給大家供大家參考,具體內(nèi)容如下
$(document).ready(function(){}):定義頁面裝載完成時,需要執(zhí)行的方法。
$()獲得頁面指定的節(jié)點,參數(shù)是某種CSS的選擇器。返回的是一個JQuery對象,可在其上執(zhí)行JQuery方法。
val()方法可以獲得節(jié)點的value屬性值
html()設定某個節(jié)點中的html內(nèi)容
click()相應鼠標點擊事件
keyup()相應鍵盤彈起事件
$.get()可以和服務器進行get方式的交互,注冊的callback方法會在數(shù)據(jù)回來的時候被調(diào)用,此方法會接收到代表服務器端返回數(shù)據(jù)的一個純文本的參數(shù)
addClass()removeClass()給某個節(jié)點增加或刪除一個class
解決中文亂碼問題:發(fā)送給服務器端的數(shù)據(jù)在js中做兩次encodeURI,然后在服務器端的代碼中按UTF-8的方式做一次URLDecode
主要代碼:
$.get("http://localhost:8080/JQueryStudy/UserVerify?userName=" + encodeURI(encodeURI(userName)),null, function(response){ $("#result").html(response); } )
處理的Servlet
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.linying; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 用戶名驗證Servlet * @author Ying-er * @time 2010-4-25下午08:02:08 * @version 1.0 */ public class UserVerify extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String param = request.getParameter("userName"); if (param == null || param.length() == 0) { out.println("用戶名不能為空"); } else { String userName = URLDecoder.decode(param, "UTF-8"); System.out.println(userName); if (userName.equals("Ying-er")) { out.println("用戶名[" + userName + "]已經(jīng)存在,請使用別的用戶名注冊"); } else { out.println("可以使用用戶名[" + userName + "]注冊"); } } } finally { out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet"> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; }// </editor-fold> }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
相關文章
實時監(jiān)控input框,實現(xiàn)輸入框與下拉框聯(lián)動的實例
下面小編就為大家分享一篇實時監(jiān)控input框,實現(xiàn)輸入框與下拉框聯(lián)動的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01jQuery+jsp實現(xiàn)省市縣三級聯(lián)動效果(附源碼)
這篇文章主要介紹了jQuery+jsp實現(xiàn)省市縣三級聯(lián)動效果,以完整實例形式分析了jQuery結合jsp讀取MySQL數(shù)據(jù)庫操作實現(xiàn)省市縣三級聯(lián)動效果的相關技巧,并附帶完整實例源碼供讀者下載參考,需要的朋友可以參考下2015-12-12jQuery實現(xiàn)的動態(tài)伸縮導航菜單實例
這篇文章主要介紹了jQuery實現(xiàn)的動態(tài)伸縮導航菜單,實例分析了jQuery鼠標事件及animate、hide等方法的使用技巧,需要的朋友可以參考下2015-05-05