Bkjia.com documentAfter reading ajax for a long time, there are few examples by hand. Today, I decided to write one by myself. The function is very simple and a simple user name registration function is implemented, at the same time, the user input is verified on the server side, and the user name is not saved in the database. application · object simulation is used here. After a long time, I got it done. I encountered a lot of problems and finally solved them. The interesting thing is that after I wrote the example, I ran normally in firefox, but I had a problem in ie, the registered user name still shows that the registration was successful. After checking for a long time, I did not figure out the reason. Finally, I reviewed a video tutorial I had read before and realized that it was caused by the cache of IE, it's easy to cheat ie with a trick! I feel very rewarding!
The following code is used:
Html code of the registration page:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Tip: the code can be modified before running!
Server asp code:
Reference content is as follows: <% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% String type = request. getParameter ("type "); If (type. equals ("username ")){ String username = request. getParameter ("username ");
If (username! = Null & username. length ()> 0 ){ If (application. getAttribute (username )! = Null ){
Out. println ("Username is exist! "); } Else { Out. println ("Username is OK! "); }
} Else { Out. println ("Username is need! "); }
} Else if (type. equals ("password ")){ String password = request. getParameter ("password "); If (password! = Null & password. length ()> 0 ){ Out. println ("password is OK ");
} Else { Out. println ("Password is need! ");
}
} Else if (type. equals ("password2 ")){ String password2 = request. getParameter ("password2 "); If (password2! = Null & password2.length ()> 0 ){ String password = request. getParameter ("password "); If (password2.equals (password )){ Out. println ("Password2 is OK! "); } Else {
Out. println ("Password2 is not equal Password! "); }
} Else { Out. println ("Password2 is need! ");
}
} Else if (type. equals ("submit ")){
Boolean regFlag = true;
String username = request. getParameter ("username "); String password = request. getParameter ("password "); String password2 = request. getParameter ("password2 ");
If (username! = Null & username. length ()> 0 ){
If (username! = Null & username. length ()> 0 ){ If (application. getAttribute (username )! = Null ){ RegFlag = false; Out. println ("username is exist! "); } }
} Else { Out. println ("Username is needed! <Br/> "); RegFlag = false; }
If (password! = Null & password. length ()> 0 ){
} Else { Out. println ("Password is need! <Br/> "); RegFlag = false;
} If (password2! = Null & password2.length ()> 0 ){
If (password2.equals (password )){
} Else {
Out. println ("Password2 is not equal Password! <Br/> "); RegFlag = false; }
} Else { Out. println ("Password2 is need! <Br/> "); RegFlag = false;
} If (regFlag = true ){ Application. setAttribute (username, username );
Out. println ("Register is successful! ");
}
} %> |