Due to the teacher assignment needs, when adding the administrator, to implement the validation of the added administrator's username is already in the database, and then the client to the user a hint. The first thing I think of is the use of Ajax to implement asynchronous authentication technology, because of the use of the SSH framework, so in this to struts2 and Ajax integration, because I have not summed up some of the Ajax knowledge, so do not mention that the Ajax details will be written in the future blog. Now, let's just do this. Add an administrator to verify that the administrator's username exists to say this Struts2+ajax implements asynchronous authentication techniques.
First, let's look at our form form:
[HTML]View Plaincopyprint?
- <TD>
- User name
- </td>
- <TD>
- <input type="text" name= "admin.username" value= ""
- Onblur="Checkusername (This, ' adminaction!exists ')" />
- </td>
- <TD>
- <span id= "namemessage" style="color:red;" ></span>
- </td>
- </tr>
- <tr bgcolor="#d6fdd0">
- <TD>
- Password
- </td>
- <TD>
- <input type="password" name= "admin.password" value= "" />
- </td>
- <TD>
- <span></span>
- </td>
- </tr>
[JavaScript]View Plaincopyprint?
- <script type="Text/javascript" >
- var xmlhttprequest = null; //Declare an empty object to receive the XMLHttpRequest object
- function checkusername (field, URL) {
- var uername = Field.value;
- if (Uername = = "" | | Uername.length < 3) {
- document.getElementById ("Namemessage"). InnerHTML = "User name should be no less than 3 bits";
- Return
- } Else {
- if (window. ActiveXObject) //IE browser
- {
- XMLHttpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
- } Else if (window. XMLHttpRequest) //Other browser implementations other than IE
- {
- XMLHttpRequest = new XMLHttpRequest ();
- }
- if (null! = XMLHttpRequest) {
- When using the Get method to access the server side with parameters, directly after the "Ajaxservlet" parameter, the following send method is a parameter null can be, with the Post method this must be added to the parameters in the send parameter, as follows
- Xmlhttprequest.open ("Get", url+"? admin.username=" +uername, true);
- A callback function that associates Ajax well
- Xmlhttprequest.onreadystatechange = Ajaxcallback;
- Really send data to the server side
- Use post to submit, you must add the following line, the Get method does not have to add this sentence
- Xmlhttprequest.setrequestheader ("Content-type",
- "application/x-www-form-urlencoded");
- Xmlhttprequest.send (null);
- }
- }
- }
- function Ajaxcallback () { //ajax a request will change four times, so we will be working on the fourth time (i.e. the end of a request) OK,
- if (xmlhttprequest.readystate = = 4) { //request succeeded
- if (Xmlhttprequest.status = = 200) {
- var responsetext = Xmlhttprequest.responsetext;
- document.getElementById ("Namemessage"). InnerHTML = ResponseText;
- }
- }
- }
- </script>
Through the above comments I think you should be able to understand some of the content, we first go to verify that the content is empty, if it is empty to the user to prompt. If not empty then go to judge the current browser, and then according to the browser to set the XMLHttpRequest object, XMLHttpRequest object is what? The XMLHttpRequest object is used in the background to exchange data with the server object, his main role:
· Update a Web page without reloading the page
· Request data from the server after the page has been loaded
· Receiving data from the server after the page has been loaded
· Sending data to the server in the background
XMLHttpRequest is the core object of Ajax, and it has several important methods or properties:
Open (): establishes a new request to the server.
Send (): Sends a request to the server.
Abort (): Exits the current request.
ReadyState: Provides the ready state of the current HTML.
ResponseText: The request response text returned by the server.
Where the XMLHttpRequest object's open () method has the following five parameters:
Request-type: type of Send request. A typical value is GET or POST, but you can also send a HEAD request.
URL: The URL to connect to.
Asynch: True if you want to use an asynchronous connection, false otherwise. This parameter is optional and is true by default.
Username: If authentication is required, you can specify the user name here. The optional parameter does not have a default value.
Password: If authentication is required, you can specify the password here. The optional parameter does not have a default value.
The first three parameters are usually used. In fact, even if you need an asynchronous connection, you should specify "true" for the third argument. This is the default value, but it is easier to understand whether the request is asynchronous or synchronous.
After we get the XMLHttpRequest object, we use this object to execute our request in the background, and we must pay attention to our callback function when we execute our request: Xmlhttprequest.onreadystatechange = Ajaxcallback, the name of the callback function here can be random, not fixed dead. We can see the above program request is sent to the adminaction in the Exists method, OK, let's go to the action method inside to see:
[Java]View Plaincopyprint?
- Public String exists () throws exception{
- SYSTEM.OUT.PRINTLN (admin==null);
- Boolean boo=dao.exists (Admin.getusername ());
- //Gets the original PrintWriter object to output the response result without jumping to an attempt to
- HttpServletResponse response = Servletactioncontext.getresponse ();
- //Set character sets
- Response.setcharacterencoding ("UTF-8");
- PrintWriter out = Response.getwriter ();
- if (boo) {
- //Input The content of the response directly
- OUT.PRINTLN ("* User name already exists *");
- /** formatted output time **/
- Out.flush ();
- Out.close ();
- }
- OUT.PRINTLN ("* User name available *");
- return null;
- }
The students who are familiar with Ajax should see this code very clearly. Here is the main use of printwriter to our backstage information output to our front desk, here I will not explain in detail. Well, here we are, this is our asynchronous validation using the Struts2+ajax implementation. Here is the specific implementation effect:
Struts2+ajax Implementing asynchronous validation