The login submission form is verified and forwarded using the POST request;
Enter user name (correct) and password (whether empty) to authenticate with GET request; 1. Login Screen and homepage
<!--login.jsp-->
<form action= "Login" method= "POST" >
<div>
<input type= "text" Name= "username" id= "userId" placeholder= "user name" >
<span class= "info" ></span>
</div>
<div>
<input type= "password" name= "password" id= "passwd" placeholder= "password" >
</div>
<input type= "Submit" value= "Login " id= "Submit" >
<span class= "hint" >${error} </ span>
</form>
<!--index.jsp-->
2. The Ajax engine requests the server and accepts the response
JavaScript var xmlHttp;
var UserID = document.getElementById ("UserId");
var passwd = document.getElementById ("passwd");
var info = document.getelementsbyclassname ("info");
var submit = document.getElementById ("submit");
var hint = document.getelementsbyclassname ("hint") [0];
Userid.onblur = CheckUser;
Passwd.onblur = checkpwd; function Createxmlhttprequest () {//check whether the ActiveXObject control (IE browser) is supported if (window).
ActiveXObject) {xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); } else if (window.
XMLHttpRequest) {xmlHttp = new XMLHttpRequest ();
}}//Check whether the user name is correct or exists function CheckUser () {createxmlhttprequest ();
var url = "Login?username=" +userid.value;
Xmlhttp.open ("GET", url, True);
Xmlhttp.send (NULL);
Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {
info[0].innerhtml = Xmlhttp.responsetext;
}
}; }//Check if the password is empty or null function checkpwd () {createxmlhttprequEST ();
var url = "login?password=" +passwd.value;
Xmlhttp.open ("GET", url, True);
Xmlhttp.send (NULL);
Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {
info[1].innerhtml = Xmlhttp.responsetext;
}
};
}//Submit check before login, if user name or password is empty, cancel request Submit.onclick = function () {if (userid.value== "") {info[0].innerhtml = "";
info[1].innerhtml = ""; hint.innerhtml = "User name cannot be empty.
";
return false;
} else if (passwd.value== "") {info[0].innerhtml = "";
info[1].innerhtml = ""; hint.innerhtml = "Password cannot be empty.
";
return false; }
}
3. Servlet responds to requests
When username is empty, press the TAB key to lose focus by using the User name input box, at which point the GET request is triggered, and the URL suffix is:? username=,
namely: username== "", Password==null
When username is not empty, press the TAB key to lose focus by using the User name input box and trigger a GET request with the URL suffix:? username=xxx,
namely: username== "xxx", password==null
If you press the TAB key again to lose focus on the password input box, continue to get request, the URL suffix is:? password=,
namely: Username==null, password== ""
Loginservlet.java @WebServlet ("/login") public class Loginservlet extends HttpServlet {... protected void do Get (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {request.setch
Aracterencoding ("Utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Username, password may be null, may not be null if (Request.getparameter ("username") = = "") {//?username= if (Request.getparameter ("password") = = null) {out.print ("User name cannot be empty.")
");
}} else if (Request.getparameter ("username") = = null) {if (Request.getparameter ("password") = = "") { ? password= out.print ("The password cannot be empty.
");
}} else {//?username=xxx String name = Request.getparameter ("username"); if (!name.equals ("admin")) {if (RequeSt.getparameter ("password") = = null) {out.print ("User name does not exist.
"); }}}} protected void DoPost (HttpServletRequest request, HttpServletResponse resp
Onse) throws Servletexception, IOException {request.setcharacterencoding ("utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
String name = Request.getparameter ("username");
String pwd = request.getparameter ("password"); if (name.equals ("admin") && pwd.equals ("admin")) {User user = new User (NAME,PWD);
User Object HttpSession session = Request.getsession ();
Session.setattribute ("user", user);
RequestDispatcher rd = Request.getrequestdispatcher ("/");
Rd.forward (request, response); } else {Request.setattribute ("error", "User name or password is incorrect.")
"); RequestDispatcher rd = Request.getreqUestdispatcher ("login.jsp");
Rd.forward (request, response); }
}
}
User.java public
class User {
String name;
String pwd;
Public User (string name, string pwd) {
this.name = name;
This.pwd = pwd;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public String getpwd () {
return pwd;
}
public void SetPwd (String pwd) {
this.pwd = pwd;
}
}
Cond...