When the user registered to use ajax, ajax user name to test the use of user registration, user name test

When users register to use ajax, user name test

package cn.hopetesting.com.servlet;2901583663

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.print.attribute.standard.JobOriginatingUserName;
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;
import java.util.HashMap;
import java.util.Map;

/**
* @author newcityman
* @date 2019/9/18 - 21:49
*/
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
void the doPost protected (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
the response.setContentType ( "file application / JSON; charset = UTF-. 8");
// the response.setContentType ( "text / HTML; charset = UTF-. 8") ;
// 1, acquiring parameters username
String = request.getParameter username ( "username");
// 2, the user calls the service layer determines whether there is a name
// expected server response data back to the format: {
// ( "userExist: to true , "msg", "this username is too popular, replace a"),
// ( "userExist: false," msg "," this username is available ")}
the Map <String, Object> = the Map new new HashMap < String, Object> ();
IF ( "tom."equals(username)){
//存在
map.put("userExist",true);
map.put ( "msg", "user name is too popular, replace a");
} the else {
// does not exist
map.put ( "userExist", to false);
map.put ( "msg", "this username available ");
}
// convert the map json, and passed to the client
// will map into JSON
ObjectMapper new new ObjectMapper Mapper = ();
// and delivered to the client
mapper.writeValue (response.getWriter ( ), Map);

}

protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
this.doPost (Request, Response);
}
}



<! DOCTYPE HTML> 
<HTML lang = "EN">
<head>
<Meta charset = "UTF-. 8">
<title> Register </ title>
<Script the src = "JS / jQuery-3.3.1.min.js "> </ Script>
<Script>
// page finishes loading
$ (function () {
// username bind to blur time
$ (" # username "). blur (function () {
// get username text entry box the value of
var username = $ (the this) .val ();
// send ajax request
// expected server response data back to the format: {
// ( "userExist: to true," MSG "," user name is too popular, replace a "),
// (" userExist: false, "msg", "this username is available")}
$.get("findUserServlet",{username:username},function (data) {
//判断userExist键的值是否存在

var span = $("#s_username");
if(data.userExist){
//存在
span.css("color","red");
span.html(data.msg);
}else{
//不存在
span.css("color","green");
span.html(data.msg);
}
}/*,"json"*/);
});
});
</script>
</head>
<body>
<input type="text" id="username" name="username"placeholder = "Please enter a username">
<span ID = "s_username"> </ span> a
<input type="password" id="password" name="passowrd" placeholder="请输入密码"><br/>
<input type="submit" value="提交">
</body>
</html>

 
package cn.hopetesting.com.servlet;2901583663

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.print.attribute.standard.JobOriginatingUserName;
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;
import java.util.HashMap;
import java.util.Map;

/**
* @author newcityman
* @date 2019/9/18 - 21:49
*/
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
void the doPost protected (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
the response.setContentType ( "file application / JSON; charset = UTF-. 8");
// the response.setContentType ( "text / HTML; charset = UTF-. 8") ;
// 1, acquiring parameters username
String = request.getParameter username ( "username");
// 2, the user calls the service layer determines whether there is a name
// expected server response data back to the format: {
// ( "userExist: to true , "msg", "this username is too popular, replace a"),
// ( "userExist: false," msg "," this username is available ")}
the Map <String, Object> = the Map new new HashMap < String, Object> ();
IF ( "tom."equals(username)){
//存在
map.put("userExist",true);
map.put ( "msg", "user name is too popular, replace a");
} the else {
// does not exist
map.put ( "userExist", to false);
map.put ( "msg", "this username available ");
}
// convert the map json, and passed to the client
// will map into JSON
ObjectMapper new new ObjectMapper Mapper = ();
// and delivered to the client
mapper.writeValue (response.getWriter ( ), Map);

}

protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
this.doPost (Request, Response);
}
}



<! DOCTYPE HTML> 
<HTML lang = "EN">
<head>
<Meta charset = "UTF-. 8">
<title> Register </ title>
<Script the src = "JS / jQuery-3.3.1.min.js "> </ Script>
<Script>
// page finishes loading
$ (function () {
// username bind to blur time
$ (" # username "). blur (function () {
// get username text entry box the value of
var username = $ (the this) .val ();
// send ajax request
// expected server response data back to the format: {
// ( "userExist: to true," MSG "," user name is too popular, replace a "),
// (" userExist: false, "msg", "this username is available")}
$.get("findUserServlet",{username:username},function (data) {
//判断userExist键的值是否存在

var span = $("#s_username");
if(data.userExist){
//存在
span.css("color","red");
span.html(data.msg);
}else{
//不存在
span.css("color","green");
span.html(data.msg);
}
}/*,"json"*/);
});
});
</script>
</head>
<body>
<input type="text" id="username" name="username"placeholder = "Please enter a username">
<span ID = "s_username"> </ span> a
<input type="password" id="password" name="passowrd" placeholder="请输入密码"><br/>
<input type="submit" value="提交">
</body>
</html>

Guess you like

Origin www.cnblogs.com/dsgs447/p/11701243.html