This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14446|Reply: 1

[JavaEE] ajax goes to the database to verify that the username is registered without submitting the form

[Copy link]
Posted on 12/24/2014 2:28:19 AM | | | |
[mw_shl_code=javascript,true]$(function(){
        $("#userName").blur(function() {
                var value = $("#userName").val();
                verifyUserName(value);
        });
        $("#userName").focus(function() {
                $("#userNameErr").text("");
        });
});

function verifyUserName(userName){
        var value = $("#userName").val();
        $.ajax({
                type:"GET",
                cache:false,
                async:false,
                url:"/bookShopping/UserServlet",
                data:{method:"verifyUserName",userName:value},
                dataType:"json",
                success:function(result){
                        if(result.ifreg =="true"){
                                $("#userNameErr").text("This username is registered!") );
                        }
        }
        });
}[/mw_shl_code]




[mw_shl_code=java,true] <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>

    <title>Registration page</title>
   
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">   
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script>
        <script type="text/javascript" src="<c:url value='/js/register.js'/>"></script>
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
<script type="text/javascript">  
function _hyz() {
        $("#vCode").attr("src", '<c:url value='/VerifyCodeServlet?'/>' + new Date().getTime());
}
</script>
  </head>
  
  <body>
  <form action="/bookShopping/UserServlet">
  <input type="hidden" name="method" value="register"/>
           Username<br/>
        <input id = "userName" name="userName" type="text" value="${formuser.userName }"/><label id = "userNameErr">${errors.loginname }</label><br/>
        Real name<br/>
        <input name="trueName" type="text" value="${formuser.trueName }" />${errors.trueName }<br/>
        Password<br/>
        <input name="passWord" type="password" value="${formuser.passWord }"/>${errors.loginpass }<br/>
          Confirm the password<br/>
        <input name="repassWord" type="password" value="${reloginpass }" />${errors.reloginpass }<br/>
        City<br/>
        <input name="city" type="text" value="${formuser.city }"/>${errors.city }<br/>
        Residential address<br/>
        <input name="address" type="text" value="${formuser.address }"/>${errors.address }<br/>
        Zip code<br/>
        <input name="postcode" type="text" value="${formuser.postcode }" />${errors.postcode }<br/>
        ID number<br/>
        <input name="cardno" type="text" value="${formuser.cardno }"/>${errors.cartno }<br/>
        Types of <br/>documents
        <input name="cardType" type="text" value="${formuser.cardType }"/>${errors.cardType }<br/>
       Contact number<br/>
        <input name="tel" type="text" value="${formuser.tel }"/>${errors.tel }<br/>
      Email <br/>
        <input name="email" type="text" value="${formuser.email }"/>${errors.email }<br/>
      CAPTCHA <br/><img id= "vCode" src="<c:url value='/VerifyCodeServlet'/>">${errors.verifyCode }
     <label> <a href="javascript:_hyz()" > replace one</a></label><br/>
        <input name="verifyCode" /><br/>
         <input name="BtnRegister" type="submit" value="Register" />
        <a href= "<c:url value='/index.jsp'/>" > return to the home page</a>
        </form>
  </body>
</html>
[/mw_shl_code]



[mw_shl_code=java,true]/**
* AJAX verifies whether the username is registered
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
        public String verifyUserName(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
        boolean b = false;
                try {
                        int i = userService.verifyUserName(req.getParameter("userName"));
                        if (i==1) {
                                b=true;
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
Returns data in JSON format               
                //resp.getWriter().print("{\"bool\":\"qqq\"}");
                resp.getWriter().print("{\"ifreg\":\""+b+"\"}");
                //resp.getWriter().print(b);
System.out.println("{\"ifreg\":\""+b+"\"}");
                return  "";
        }[/mw_shl_code]



1.png (226.76 KB, Number of downloads: 426)

2.png (85.65 KB, Number of downloads: 428)

3.png (93.44 KB, Number of downloads: 412)

4.png (174.62 KB, Number of downloads: 418)

5.png (77.35 KB, Number of downloads: 412)

6.png (132.41 KB, Number of downloads: 387)

7.png (113.82 KB, Number of downloads: 397)





Previous:struts2 custom time converter
Next:struts2 to get the commonly used objects in the servlet
Posted on 12/24/2014 4:27:25 PM |
I'll copy it as an assignment
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com