Ajax writing User Registration examples and technical summary (2)

Source: Internet
Author: User
Tags object error handling http request string window
Ajax| User Registration

After using JavaScript to create a XMLHttpRequest class to send an HTTP request to the server , decide what to do when you receive a response from the server . this tells the HTTP request object which JavaScript function to use to handle the response . you can set the object's onReadyStateChange property to the function name of the JavaScript you want to use, as follows: Xmlhttp_request.onreadystatechange =functionname;

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 parameter and takes the URL of the data provider as the second parameter Request.open () The last parameter 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 .) )
Then 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); Look forDatabaseThere is no such user name in

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); Look forDatabaseIs there any business name in the

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

    • Ajax: A new way to build Web apps
    • Discussion on the error handling mechanism of AJAX (2)
    • Discussion on the error handling mechanism of AJAX (1)
    • First experience. NET Ajax Brushless New technology
    • A brief analysis of Ajax development Technology in Rails system (4)


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.