Ajax js 使用Ajax检测用户名是否存在

前端之家收集整理的这篇文章主要介绍了Ajax js 使用Ajax检测用户名是否存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
jsp页面:
Js代码
  1. <scripttype="text/javascript"language="javascript">
  2. varxmlHttp=false;
  3. try{
  4. xmlHttp=newActiveXObject("Msxml2.XMLHTTP");
  5. }catch(e){
  6. try{
  7. xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
  8. }catch(e2){
  9. xmlHttp=false;
  10. }
  11. }
  12. if(!xmlHttp&&typeofXMLHttpRequest!='undefined'){
  13. xmlHttp=newXMLHttpRequest();
  14. }
  15. //检测用户名是否存在
  16. functioncallServerCheckUname(){
  17. //通过GET方法请求,将请求封装在URL中
  18. varuname=document.getElementById("u_name").value;
  19. xmlHttp.open("POST",'CheckUname?u_name='+uname);
  20. //设置服务器响应的处理方法
  21. xmlHttp.onreadystatechange=updatePageCheckUname;
  22. //发送请求
  23. xmlHttp.send(null);
  24. }
  25. functionupdatePageCheckUname(){
  26. if(xmlHttp.readyState==4){
  27. varresponse=xmlHttp.responseText;
  28. if(response>=1){
  29. alert("用户名已存在!");
  30. }else{
  31. alert("可注册!");
  32. }
  33. }
  34. }
  35. </script>


web.xml配置文件:
Xml代码
@H_100_301@
  • <servlet>
  • <servlet-name>CheckUname</servlet-name>
  • <servlet-class>servlet.AjaxCheckUname</servlet-class>
  • </servlet>
  • <servlet-mapping>
  • <servlet-name>CheckUname</servlet-name>
  • <url-pattern>/CheckUname</url-pattern>
  • </servlet-mapping>


  • servlet页面:
    Java代码
    1. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
    2. throwsServletException,IOException{
    3. response.setContentType("text/html;charset=GB2312");
    4. PrintWriterout=response.getWriter();
    5. try{
    6. Stringuname=request.getParameter("u_name");
    7. RegistDAOImplregDao=newRegistDAOImpl();
    8. out.print(regDao.checkUname(uname));
    9. }catch(Exceptione){
    10. }
    11. out.flush();
    12. out.close();
    13. }


    DAO实现类:
    Java代码
    1. publicintcheckUname(Stringuname){
    2. Connectionconn=null;
    3. PreparedStatementpstm=null;
    4. ResultSetrs=null;
    5. intsuccess=0;
    6. Stringsql="selectu_namefromu_infowhereu_name='"+uname+"'oru_mobtel='"+uname+"'oru_email='"+uname+"'";
    7. try{
    8. conn=getConnection();
    9. pstm=conn.prepareStatement(sql);
    10. rs=pstm.executeQuery(sql);
    11. if(rs.next()){
    12. success=1;
    13. }
    14. returnsuccess;
    15. }catch(Exceptione){
    16. e.printStackTrace();
    17. }finally{
    18. try{
    19. conn.close();
    20. }catch(Exceptione){
    21. e.printStackTrace();
    22. }
    23. }
    24. returnsuccess;
    25. }
    原文链接:https://www.f2er.com/ajax/166798.html

    猜你在找的Ajax相关文章