Using AJAX to write user registration examples and technical summary (2)

Source: Internet
Author: User
Tags add http request string window
Ajax| User Registration Author: Dragon Calf finishing Source: Tenkine responsible Editor: Dragon calf [2006-03-17 09:12] Use Ajax to write a user registration instance--the example I have cited is an application of an enterprise user registration, checking that the user name and enterprise name are available when the user registers. The previous practice is to add a button next to, click "Check", the server issued a request, and then wait ... The server returns information and continues the operation.

FunctionName is a function name created with JavaScript, be careful not to write functionname (), and of course we can create JavaScript code directly after onreadystatechange.
We call Request.open ()-It opens the socket channel with the server, uses an HTTP verb (GET or post) as the first argument and the URL of the data provider as the second argument. The last parameter of Request.open () is set to true-it indicates the asynchronous nature of the request. Note that the request has not been submitted yet. With the invocation of Request.send (), the commit is initiated-this can provide any necessary payload for post. When using asynchronous requests, we must use the Request.onreadystatechanged property to allocate the requested callback function. (If the request is synchronized, we should be able to process the result immediately after calling Request.send, but we can also block the user until the request is complete.) )

I'll take a look at the data provider's Url,url = "/chkuserandcom", the servlet is as follows:

1/**//*
2 * Created on 2005-12-31
3 *
4 * TODO To change the template of this generated file go
5 * Window-preferences-java-code Style-code Templates
6 */
7package com.event;
8
9import javax.servlet.ServletException;
10import javax.servlet.http.HttpServletRequest;
11import Javax.servlet.http.HttpServletResponse;
12
13import Com.beans.EBaseInfo;
14
15/** *//**
* @author Alpha 2005-12-31
17 *
* <p> Ajax Demo---Enterprise registration check enterprise user name and enterprise name </P>
19 *
* TODO to change the template for this generated type comment go
* Window-preferences-java-code Style-code Templates
22 */
23public class Checkuserandcomnm {
Private String msgstr = "";
protected void doget (HttpServletRequest request,httpservletresponse response)
Num throws Servletexception
27 {
28
Ecombaseinfo info=new ecombaseinfo ();
String oprate=request.getparameter ("Oprate")). Trim ();
To String username=request.getparameter ("UserName");
Password=request.getparameter String ("PassWord");
Comname=request.getparameter String ("Comname");
34
Try
36 {
Panax Notoginseng if (oprate.equals ("Chkuser"))
38 {
Response.setcontenttype ("text/html;charset=gb2312");
if (Username.length () <5| | Username.length () >20)
41 {
Msgstr = "Sorry, the username must be a letter, number or underscore, length is 5-20 characters!";
43}
Or else
45 {
A Boolean btmp=info.finduser (UserName); Check to see if the username is in the database
if (btmp)
Msgstr = "Sorry, this username already exists, please change user name registration!";
Or else
Msgstr = "";
51}
Response.getwriter (). write (MSGSTR);
53}
"Else If" (Oprate.equals ("chkcom"))
55 {
Response.setcontenttype ("text/html;charset=gb2312");
if (Comname.length () <6| | Comname.length () >100)
58 {
Msgstr = "Sorry, the company name length is 6-100 characters (not including the word characters space)!";
60}
Or else
62 {
A Boolean btmp=info.findcom (Comname); Check the database for the name of the enterprise
if (btmp)
Msgstr = "Sorry, this enterprise name already exists, please change the Enterprise name registration!";
Or else
Msgstr = "";
68}
Response.getwriter (). write (MSGSTR);
70
71}
72}
catch (Exception ex)
74 {
75}
and finally
77 {
Request.setattribute ("url", url);
79}
80}
81
protected void DoPost (HttpServletRequest request,httpservletresponse response)
Throws Servletexception
84 {
Doget (Request,response);
86}
87}
88

   A summary of Ajax technology

1. Ajax (asynchronous JavaScript and XML) is a combination of Java technology, XML, and JavaScript programming techniques that allow you to build Web applications based on Java technology and break the practice of using page overloading.

2. Ajax, asynchronous JavaScript and XML, is a Web application development method that uses client script to exchange data with a Web server. This way, Web pages can be updated dynamically without interrupting the interactive process. With Ajax, you can create a direct, highly available, richer, more dynamic Web user interface that is close to local desktop applications.

3. For Mozilla﹑netscape﹑safari, Firefox and other browsers, create XMLHttpRequest methods are as follows:

Xmlhttp_request = new XMLHttpRequest ();

4. IE and other create XMLHttpRequest methods are as follows:

XMLHTTP = new ActiveXObject ("Msxml2.xmlhttp")

or xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");

5. Xmlhttp_request.open (' Get ', URL, true); Xmlhttp_request.send (NULL);

6. The first parameter of the Open () is the method of HTTP request-get,post or the way that any server supports that you want to invoke. This argument is capitalized according to the HTTP specification, otherwise some browsers, such as Firefox, may not be able to process the request. The second parameter is the URL of the request page. The third parameter sets whether the request is asynchronous mode. If the True,javascript function continues to execute without waiting for the server to respond. This is "A" in "AJAX".

Ajax technology to use good words, to our web page to add a lot of friendly effect, to give users a better feeling. Ajax is a good thing.



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.