14.javaweb Ajax Technical Details

Source: Internet
Author: User

First, Introduction

1, Ajax: The technique of exchanging data with the server and updating some Web pages without reloading the Web page, but it is not a new technology in itself

2, Core: XMLHttpRequest object. Ajax technology is mainly done through this object.

When creating this object, be sure to consider the browser's problem

Creation of XMLHttpRequest objects

XMLHttpRequest Object Properties

Where there are five types of readystate values

XMLHttpRequest Object Methods

3, simple example: use Ajax to complete the local refresh function: Local refresh, import content.html content in <span>

content.html Content

Flush.html

Xmlhttp.onreadystatechange=showmsgcallback: The Showmsgcallback () method is called when XmlHttp's request state changes, in fact, when the Xmlhttp.send () When the request is sent to the response, ReadyState will experience "2" (Request Processing), "3" (Request processed), "4" (response complete) three changes, and if the callback function is set before Xmlhttp.send (), it will call three times Showmsgcallback () Method. We do this when we set up readystate=4 because the response is complete and the server populates the response with the xmlhttp.response*** property

Clicking "Invoke Ajax Display" will dynamically generate "get data" in <span>

Second, Ajax asynchronous authentication: When the new user registration information, enter the ID, the asynchronous authentication ID is available

Login.html

Note how address rewriting is used here

Checkservlet

 Packagecom.chen.ying;ImportJava.io.PrintWriter;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCheckidextendsHttpServlet {FinalString dbdivre= "Com.mysql.jdbc.Driver"; FinalString url= "Jdbc:mysql://localhost:3306/test"; FinalString user= "Root"; FinalString password= "1995228"; Connection Conn=NULL; PreparedStatement pstmt=NULL; ResultSet RS=NULL;  Public voiddoget (httpservletrequest req,httpservletresponse res) {intUserid=integer.parseint (Req.getparameter ("userid")); Try{System.out.println (userid); PrintWriter out=res.getwriter ();//the output stream that gets the responseClass.forName (Dbdivre); Conn=drivermanager.getconnection (Url,user,password); String SQL= "Select UserID from the person where userid=?"; Pstmt=conn.preparestatement (SQL); Pstmt.setint (1, UserID); RS=Pstmt.executequery (); if(Rs.next ()) {System.out.println ("Duplicate User ID"); Out.print ("True"); }            Else{System.out.println ("ID Available"); Out.print ("False"); }        }Catch(Exception e) {System.out.println (e); }          }     Public voiddoPost (httpservletrequest req,httpservletresponse res) { This. Doget (req, res); }}

Effect

When the user enters the ID, the cursor leaves the output box and, using AJAX technology, asynchronously verifies that the input ID is duplicated

Third, return XML data

1, requirements

14.javaweb Ajax Technical Details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.