加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用ajax实现检测注册用户名是否重复的完整例子(一)

发布时间:2020-12-16 00:26:28 所属栏目:百科 来源:网络整理
导读:网上搜索了很多案例,但都和自己的需求不一样。 最终实现了下面的例子实现了对注册用户名的校验。采用的架构是servelet+jsp.ValidateName.java代码如下所示,并且采用userIsExist查找数据库看是否存在相同的用户名。 Java代码 1. package com.wuliu.test; 2.

网上搜索了很多案例,但都和自己的需求不一样。
最终实现了下面的例子实现了对注册用户名的校验。采用的架构是servelet+jsp.ValidateName.java代码如下所示,并且采用userIsExist查找数据库看是否存在相同的用户名。

Java代码

1. packagecom.wuliu.test;

2. importjava.io.IOException;

3. importjava.io.PrintWriter;

4.

5. importjavax.servlet.ServletException;

6. importjavax.servlet.http.HttpServlet;

7. importjavax.servlet.http.HttpServletRequest;

8. importjavax.servlet.http.HttpServletResponse;

9. importcom.wuliu.dao.LoginDAO;

10.

11.publicclassValidateNameextendsHttpServlet{

12.publicValidateName(){

13.super();

14.}

15.

16.publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

17.throwsServletException,IOException{

18.

19.response.setContentType("text/html");

20.LoginDAOdao=newLoginDAO();

21.booleanflag=false;

22.StringloginName=request.getParameter("loginName").toString();

23.flag=dao.userIsExist(loginName);

24.if(true==flag)

25.{

26.response.getWriter().write("true");//此值jquery可以接收到

27.}

28.}

29.

30.

31.publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

32.throwsServletException,IOException{

33.

34.this.doGet(request,response);

35.

36.}

37.

38.}

Java代码

1. publicbooleanuserIsExist(StringloginId){

2. System.out.println("EnteruserIsExist");

3. this.dao=newDBConnection();

4. this.cn=this.dao.getConnection();

5. //根据指定用户名查询用户信息

6. Stringsql="select*fromLoginTablewhereLoginId='"+loginId+"'";

7. System.out.println("logid:"+loginId);

8. try{

9. //获取PreparedStatement对象

10.this.ps=this.cn.prepareStatement(sql);

11.//对用户对象属性赋值

12.//ps.setString(1,loginId);

13.//执行查询获取结果集

14.rs=this.ps.executeQuery();

15.//判断结果集是否有效

16.//System.out.println("rs.next()="+rs.next());

17.if(false==rs.next()){

18.//如果无效则证明此用户名可用

19.System.out.println("用户名可用");

20.returntrue;

21.}

22.//释放此ResultSet对象的数据库和JDBC资源

23.rs.close();

24.//释放此PreparedStatement对象的数据库和JDBC资源

25.ps.close();

26.}catch(SQLExceptione){

27.e.printStackTrace();

28.}finally{

29.//关闭数据库连接

30.this.dao.closeConnection(cn);

31.}

32.System.out.println("用户名不可用");

33.returnfalse;

34.}



由此就可以实现注册的时候,校验注册用户名是否已经存在的功能了。
JSP页面代码:

Html代码

1. <formaction="register.do?action=add"onsubmit="returnsubmessage(this)"method="post"name="form1">

2. <tableborder="1"width="500"cellspacing="1"cellpadding="3"align="left"bordercolor="#326598">

3. <tr>

4. <tdcolspan="7"bgcolor="#FEA817">

5. [align=center]

6. <fontcolor="#FFFFFF"><b>用户注册</b></font>

7. [/align]

8. </td>

9. </tr>

10. <tr>

11. <td>

12. 用户名

13. </td>

14. <td>

15. <inputname="uname"id="username"type="text"class="form_text"size="20"onblur="validatorloginName()">

16. </td>

17. </tr>

18.

19. <tr>

20. <td>

21. 登陆密码

22. </td>

23. <td>

24. <inputtype="password"name="upwd">

25. </td>

26. </tr>

27. <tr>

28. <td>

29. 确认密码

30. </td>

31. <td>

32. <inputtype="password"name="upwd1">

33. </td>

34. </tr>

35. <tr>

36. <tdcolspan="2"align="center">

37. <inputtype="submit"value="提交">

38. <inputtype="reset"value="重置">

39. </td>

40. </tr>

41. </table>

42. </form>


页面上添加的ajax组件:

Html代码

1. functionvalidatorloginName(){

2. varloginName=document.getElementById("uname").value;

3. if(loginName=="")

4. {

5. alert("用户名不能为空!");

6. return;

7. }

8. $.ajax({

9. type:"POST",

10. url:"ValidateName",

11. data:"loginName="+loginName,

12. success:function(data){

13. if(data=="true"){

14. alert("恭喜您!用户名没有被使用!");

15.

16. }else{

17. alert("抱歉!用户名已存在!");

18. }

19. }

20. });

21. }


通过ajax将注册用户名发送到ValidateName.do进行校验。

web.xml里面配置如下:

Xml代码

1. <servlet>

2. <description>ThisisthedescriptionofmyJ2EEcomponent</description>

3. <display-name>ThisisthedisplaynameofmyJ2EEcomponent</display-name>

4. <servlet-name>ValidateName</servlet-name>

5. <servlet-class>com.wuliu.test.ValidateName</servlet-class>

6. </servlet>

7.

8. <servlet-mapping>

9. <servlet-name>ValidateName</servlet-name>

10. <url-pattern>/ValidateName</url-pattern>

11. </servlet-mapping>

ValidateName.java代码如下所示,并且采用userIsExist查找数据库看是否存在相同的用户名。

Java代码

1. packagecom.wuliu.test;

2. importjava.io.IOException;

3. importjava.io.PrintWriter;

4.

5. importjavax.servlet.ServletException;

6. importjavax.servlet.http.HttpServlet;

7. importjavax.servlet.http.HttpServletRequest;

8. importjavax.servlet.http.HttpServletResponse;

9. importcom.wuliu.dao.LoginDAO;

10.

11. publicclassValidateNameextendsHttpServlet{

12. publicValidateName(){

13. super();

14. }

15.

16. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

17. throwsServletException,IOException{

18.

19. response.setContentType("text/html");

20. LoginDAOdao=newLoginDAO();

21. booleanflag=false;

22. StringloginName=request.getParameter("loginName").toString();

23. flag=dao.userIsExist(loginName);

24. if(true==flag)

25. {

26. response.getWriter().write("true");//此值jquery可以接收到

27. }

28. }

29.

30.

31. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

32. throwsServletException,IOException{

33.

34. this.doGet(request,response);

35.

36. }

37.

38. }



Java代码

1. publicbooleanuserIsExist(StringloginId){

2. System.out.println("EnteruserIsExist");

3. this.dao=newDBConnection();

4. this.cn=this.dao.getConnection();

5. //根据指定用户名查询用户信息

6. Stringsql="select*fromLoginTablewhereLoginId='"+loginId+"'";

7. System.out.println("logid:"+loginId);

8. try{

9. //获取PreparedStatement对象

10. this.ps=this.cn.prepareStatement(sql);

11. //对用户对象属性赋值

12. //ps.setString(1,loginId);

13. //执行查询获取结果集

14. rs=this.ps.executeQuery();

15. //判断结果集是否有效

16. //System.out.println("rs.next()="+rs.next());

17. if(false==rs.next()){

18. //如果无效则证明此用户名可用

19. System.out.println("用户名可用");

20. returntrue;

21. }

22. //释放此ResultSet对象的数据库和JDBC资源

23. rs.close();

24. //释放此PreparedStatement对象的数据库和JDBC资源

25. ps.close();

26. }catch(SQLExceptione){

27. e.printStackTrace();

28. }finally{

29. //关闭数据库连接

30. this.dao.closeConnection(cn);

31. }

32. System.out.println("用户名不可用");

33. returnfalse;

34. }

由此就可以实现注册的时候,校验注册用户名是否已经存在的功能了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读