JQuery用户名校验的具体实现
本实例为大家分享了JQuery用户名校验功能,分享给大家供大家参考,具体内容如下
$(document).ready(function(){}):定义页面装载完成时,需要执行的方法。
$()获得页面指定的节点,参数是某种CSS的选择器。返回的是一个JQuery对象,可在其上执行JQuery方法。
val()方法可以获得节点的value属性值
html()设定某个节点中的html内容
click()相应鼠标点击事件
keyup()相应键盘弹起事件
$.get()可以和服务器进行get方式的交互,注册的callback方法会在数据回来的时候被调用,此方法会接收到代表服务器端返回数据的一个纯文本的参数
addClass()removeClass()给某个节点增加或删除一个class
解决中文乱码问题:发送给服务器端的数据在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 + "]已经存在,请使用别的用户名注册"); } 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> }
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17