Using the jQuery framework, the underlying Ajax asynchronous technology is encapsulated, which can be achieved through simple method calls. This blog is about automatically detecting the problem of duplicate user names when users register. The technology used is Ajax asynchronous transmission. This article mainly introduces in detail jQuery Ajax to realize real-time detection of duplicate user names, and automatically detects the problem of duplicate user names when users register. It has certain reference value. Interested friends can refer to it. I hope it can Help everyone.
register.jsp Registration display page
UserServlet Logic processing Servlet class, used to determine the user's duplicate name and output the response processing results, etc. The method for detecting duplicate names in the database is omitted (LoginDao.getInstance().checkUserName(userName.trim()); // Find whether the user name exists in the database)
##
package com.servlet.user; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.LoginDao; import com.user.UserInfo; public class UserServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); // 设置响应结果的格式为text/html,字符集为UTF-8 response.setCharacterEncoding("UTF-8"); // 设置响应结果的字符编码为UTF-8 // 禁止缓存 response.setHeader("Cache-Control", "no-store,no-cache,must-revalidate"); response.setHeader("Cache-Control", "post-check=0,pre-check=0"); response.setDateHeader("Expires", 0); response.setHeader("Pragma", "no-cache"); PrintWriter out = response.getWriter(); out.println(""); // 从httpRequest()方法中获得请求参数值 // 通过httpRequest()方法封装的请求参数被编码为UTF-8格式,此处若想还原原来的编码格式,则需要通过UTF-8格式解码 String userName = request.getParameter("userName"); String result = null; boolean check = LoginDao.getInstance().checkUserName(userName.trim()); // 查找数据库是否存在该用户名 if (check) { result = "该用户已经被使用"; } else { result = "该用户名可以使用"; } response.getWriter().print(result); // 将结果输出到response响应流中 } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } }
Ajax implements asynchronous user name verification function
Ajax verification user name example code
Use AJAX to complete the asynchronous verification of the user name. Detailed explanation of the instance
The above is the detailed content of Detailed explanation of duplicate user names using jQuery and Ajax. For more information, please follow other related articles on the PHP Chinese website!