QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

查看: 3549|回复: 0

[JAVA/JSP] AJAX实现注册验证用户名

[复制链接]

等级头衔

积分成就    金币 : 2861
   泡泡 : 1516
   精华 : 6
   在线时间 : 1328 小时
   最后登录 : 2026-5-15

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2021-4-10 23:05:56 | 显示全部楼层 |阅读模式
功能说明: z9 K' L) r+ V& u5 H7 W0 U% U
       当用户在注册页面输入用户名并且鼠标焦点离开输入框时,到数据表中去验证该用户名是否已经存在,如果存在提示不可用,否则,提示可用。
4 h' O5 \/ G6 s- o' U接口3 w1 x  T. M; B1 I9 F, Y2 P
public interface UserDao {
 public User findName(String name);
}
接口实现类
, ?: A! M5 K5 l2 u5 n
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

 @Override
 public User findName(String name) {
  User user =null;
  Connection conn = DBHelper.getConn();
  String sql = "select * from user where name=?";
  try {
   PreparedStatement ps = conn.prepareStatement(sql);
   ps.setString(1,name);
   ResultSet rs = ps.executeQuery();
   if (rs.next()){
    user = new User();
    user.setId(rs.getInt(1));
    user.setName(rs.getString(2));
    user.setPassword(rs.getString(3));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return user;
 }
}
servlet5 `$ J1 [& [  w7 D  h2 U" a' n
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findName")
public class FindNameServlet extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  String name = request.getParameter("name");
  UserDao userDao = new UserDaoImpl();
  User name1 = userDao.findName(name);
  if (name1!=null){
   response.getWriter().write("1");
  }else {
   response.getWriter().write("2");
  }
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  this.doPost(request, response);
 }
}
JSP页面7 h4 j" f1 [" a6 D9 R$ C3 i1 v
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
 <title>AJAX实际操作注册验证用户名</title>
 <script src="js/jquery-1.8.3.js"></script>
 </head>
 <body>
 <form action="#" method="post">
 <script type="text/javascript">
  $(function () {
   $("[name=userName]").blur(function () {
    $.ajax({
     type:"get",
     url:"findName?name="+$("[name=userName]").val(),
     dataType:"text",
     success:function (data) {
      //alert(data);
      if (data=="1"){
       $("#show").html("用户已存在!!!")
      }else {
       $("#show").html("用户名可用")
      }
     }
    })
   })
  });
 </script>
 账号<input type="text" name="userName"><span id="show"></span></br>
 密码<input type="password" name="password"></br>
 <input type="submit" value="提交">
 </form>
 </body>
</html>
数据库如下:
3 d$ s& s5 j0 K3 R& u1 I$ }3 |) [ 1.jpg & ~: X& [: `9 n! V: ~0 d
运行结果如下:4 `, C8 V1 }0 L( r
2.jpg
! O, ~- S* b& p/ i 3.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2026-7-15 15:22

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表