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> }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgyjjs.html