利用Ajax检测用户名是否被占用的完整实例
(编辑:jimmy 日期: 2024/11/17 浏览:3 次 )
适合人群:Ajax和jQuery入门
采用Ajax实现用户名验证
使用jQuery给出提示信息
用户注册的时候,使用Ajax实现检测用户名是否已经被注册过,很多细节没有实现,给大家做个简单普及。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户注册页面</title> <script src="/UploadFiles/2021-04-02/jquery.min.js">下面是ValidateServlet模拟实现,没有做真正的数据库表数据检测,大家自行完成。
package com.ambow.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ValidateServlet") public class ValidateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ValidateServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); String account = request.getParameter("account"); System.out.println("account"+account); if("123".equals(account)) { pw.print("true"); }else { pw.print("false"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
下一篇:ajax请求添加自定义header参数代码