Ajax Simple User Registration verification

Source: Internet
Author: User

The core of Ajax is the XMLHttpRequest object
All modern browsers support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).
XMLHttpRequest is used to exchange data with the server in the background. This means that you can do this without reloading the entire Web page.
Update a section of the Web page.

Example: Simple verification of user name availability via asynchronous refresh of Ajax

Front-end JSP page code:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >    function Cheackusername () {        var userName=document.getelementbyid ("UserName"). Value;

//Create XMLHttpRequest object:
var xmlhttp;
if (window.      XMLHttpRequest)
{//compatible Ie7+,firefox,chrom,opera,safari
XMLHTTP = new XMLHttpRequest ();
      }ELSE{//IE5,IE6
xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
        Xmlhttp.onreadystatechange = function () {

if (xmlhttp.readystate==4&&xmlhttp.status==200) {                alert (xmlhttp.responsetext);                 = eval ("(" +xmlhttp.responsetext+ ")");                 if (dataobj.exist) {                    document.getElementById ("Tip"). innerhtml= "The user already exists! ";                } Else {                    document.getElementById ("Tip"). Innerhtml= "The user is available! ";                }            }        };

To send a request to the server:
Use the open () and send () methods of the XMLHttpRequest object

Xmlhttp.open ("Get", "Getinfo?action=cheackusername&username=" +username,true);    Xmlhttp.send (); }</script>

Create a servlet in the Tomcat server to impersonate the recipient and verify the user name:

The jar package required to introduce JSON: Commons-beanutils-1.7.0.jar Commons-lang-2.4.jar Json-lib-2.2.3-jdk15.jar

Commons-collections-3.2.jar Commons-logging-1.0.4.jar Ezmorph-1.0.3.jar

 PackageCom.cong.ajax;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportNet.sf.json.JSONObject; Public classGetInfoextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request, response); }    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html; Charset=utf-8 "); PrintWriter out=Response.getwriter (); String UserName=request.getparameter ("UserName"); Receive data from your browser jsonobject Resultjson=Newjsonobject ();//Create Jsonobject object, introduce necessary jar packageif("Hello". Equals (UserName)) {Resultjson.put ("Exist",true); }Else{resultjson.put ("Exist",false);        } out.println (Resultjson);        Out.flush ();    Out.close (); }}

To configure a servlet in Web. xml:

<servlet>      <Servlet-name>GetInfo</Servlet-name>      <Servlet-class>Com.cong.ajax.GetInfo</Servlet-class>  </servlet>    <servlet-mapping>      <Servlet-name>GetInfo</Servlet-name>      <Url-pattern>/getinfo</Url-pattern>  </servlet-mapping>

Ajax Simple User Registration verification

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.