Ajax compilation of user registration instances and Technical Summary 2

Source: Internet
Author: User
We call request. open ()-It uses the server to open the socket channel, uses an HTTP verb (GET or POST) as the first parameter, and uses the URL of the data provider as the second parameter. The last parameter of request. open () is set to true-it indicates the asynchronous feature of the request. Note that the request has not been submitted. With the call to request. send (), start to submit-this can provide POST with any necessary payload. When using asynchronous requests, we must use the request. onreadystatechanged attribute to allocate the request callback function. (If the request is synchronous, we should be able to process the result immediately after request. send is called, but we may also block the user until the request is completed .)

Let's take a look at the data provider URL, url = "/chkUserAndCom". The servlet is as follows:

1 /**//*
2 * created on 2005-12-31
3 *
4 * todo to change the template for this generated file go
5 * window-preferences-Java-code style-code templates
6 */
7 Package com. event;
8
9 Import javax. servlet. servletexception;
10 Import javax. servlet. http. httpservletrequest;
11 import javax. servlet. http. httpservletresponse;
12
13 Import com. Beans. ebaseinfo;
14
15 /***//**
16 * @ author Alpha 2005-12-31
17 *
18 * <P> Ajax demo --- check the enterprise user name and enterprise name during enterprise registration </P>
19 *
20 * TODO To change the template for this generated type comment go
21 * Window-Preferences-Java-Code Style-Code Templates
22 */
23 public class CheckUserAndComNm {
24 private String msgStr = "";
25 protected void doGet (HttpServletRequest request, HttpServletResponse response)
26 throws ServletException
27 {
28
29 EComBaseInfo info = new EComBaseInfo ();
30 String oprate = request. getParameter ("oprate"). trim ();
31 String userName = request. getParameter ("userName ");
32 String passWord = request. getParameter ("password ");
33 String comName = request. getParameter ("comName ");
34
35 try
36 {
37 if (oprate. equals ("chkUser "))
38 {
39 response. setContentType ("text/html; charset = GB2312 ");
40 if (userName. length () <5 | userName. length ()> 20)
41 {
42 msgStr = "sorry, the username must be a letter, number, or underline. It must be 5-20 characters long! ";
43}
44 else
45 {
46 boolean bTmp = info. findUser (userName); // check whether the user name is in the database.
47 if (bTmp)
48 msgStr = "sorry, this user name already exists. Please register with another user name! ";
49 else
50 msgStr = "";
51}
52 response. getWriter (). write (msgStr );
53}
54 else if (oprate. equals ("chkCom "))
55 {
56 response. setContentType ("text/html; charset = GB2312 ");
57 if (comName. length () <6 | comName. length ()> 100)
58 {
59 msgstr = "sorry, the company name is 6-characters long (excluding spaces )! ";
60}
61 else
62 {
63 Boolean btmp = info. findcom (comname); // check whether the enterprise name is in the database.
64 if (btmp)
65 msgstr = "sorry, this company name already exists. Please register with another company name! ";
66 else
67 msgstr = "";
68}
69 response. getwriter (). Write (msgstr );
70
71}
72}
73 catch (exception ex)
74 {
75}
76 finally
77 {
78 request. setattribute ("url", URL );
79}
80}
81
82 protected void dopost (httpservletrequest request, httpservletresponse response)
83 throws servletexception
84 {
85 doGet (request, response );
86}
87}
88

  Ajax technology Summary

1. ajax (Asynchronous JavaScript and XML) is a programming technology that integrates Java, XML, and JavaScript. It allows you to build Web applications based on Java technology, and broke the Convention of using page overloading.

2. Ajax, Asynchronous JavaScript, and XML are Web application development methods for exchanging data with Web servers using client scripts. In this way, the Web page can be dynamically updated without interrupting the interaction process and cutting it again. With Ajax, you can create direct, highly available, richer, and more dynamic Web user interfaces that are close to local desktop applications.

3. for Mozilla, Netscape, Safari, Firefox, and other browsers, create XMLHttpRequest as follows:

Xmlhttp_request = new XMLHttpRequest ();

4. The method for creating XMLHttpRequest, such as IE, is 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 open () is the HTTP Request Method-Get, post, or the method that you want to call supported by any server. According to HTTP specifications, this parameter must be capitalized; otherwise, Some browsers (such as Firefox) may not be able to process requests. The second parameter is the URL of the Request page. The third parameter sets whether the request is in asynchronous mode. If it is true, the JavaScript function will continue to be executed without waiting for the server to respond. This is "A" in "ajax ".

If Ajax technology is used well, it adds many friendly effects to our webpage and gives 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.