AJAX verifies the database content and displays the value on the page. ajax verifies the database page.
Function implementation:
Enter the text box content on the jsp page, move the cursor away from the text box, and obtain other data corresponding to the value change in the database in the corresponding area of this page.
Servlet:
Request. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); // call the servlet layer to check whether the database has the same user name and return other records on the page String client_id = request. getParameter ("client_id"); ClientServices clientServices = new ClientServices (); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} Client client = clientServices. findClientById (client_id); if (client! = Null) {out. print (URLEncoder. encode (client. getClient_name (), "UTF-8");} else {out. print ("false");} out. flush (); out. close ();
Jquery:
$ (Document ). ready (function () {$ ("# client_id "). blur (function () {$. ajax ({type: 'post', url: 'servlet/validServlet? Client_id = '+ $ (this ). val (), data: 'client _ id = '+ $ ("# client_id "). val (), success: function (msg) {if (msg = 'false') {alert ("No such person ");} else {// UTF-8 decoding solves Chinese garbled characters $ ("# clientInfo" ).html (decodeURI (msg); $ ("# clientInfo "). attr ("value", decodeURI (msg ));}}});});});
Use AJAX to connect to the database, verify that the login database accepts textbox input and displays it on another view page.
The user verifies the data submitted in the form and the information in the database, and stores the post content in the database. The post content is displayed from the database when used.
How to Use Ajax to connect to the database to verify the input value
I am engaged in java, so jsp is used here
Data Layer (sqlserver)
In dao
Public boolean checklogin (String username, String password ){
/***** The specific database driver and url should be able to handle it by yourself **/
Boolean chelogin = false;
// If the user name does not exist, false is returned. true code is omitted.
Return chelogin;
}
In doget () of UserServlet
PrintWriter out = response. getWriter ();
// Call checklogin () in dao ()
String username = request. getparameter ("username ");
String password = request. getparameter ("password ");
Boolean checklogin = new checklogin (username, password );
If (checklogin = true ){
Out. print ("User Name verification is correct, login successful"); // This is to return a text message to XMLHttpRequest. response of ajax.
}
Else {
Out. print ("the user name or password is incorrect (the user name does not exist )");
}
In page js
<Script>
Var httpreq;
// Create an XMLHttpRequest object
Function createXmlHttpReq (){
If (window. ActiveXObject ){
Httpreq = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
Httpreq = new XMLHttpRequest ();
}
}
// Send the request
Function sendinfo (){
// Obtain the user name and password on the page
Var username = document. getElementById ("username"). value;
Var password = document. getElementById ("password"). value;
// 1. Create an XMLHttpRequest object
CreateXmlHttpReq ();
// 2. Write the Request Path, set "GET" or "POST", and set "true" to "Asynchronous,
Var url = "UserServlet? Username = "+ username +" & password = "+ password;
Httpreq. open ("GET", url, true );
// 3. Set the callback function
Httpreq. onreadystatechange = callback;
// 4. Send the request
Httpreq. send (null );
}
// Callback function
Function callback (... the remaining full text>