• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Ajax and Js Quest Analog Java Implementation ...

Ajax in the U.S. has not used mock consider how easy the ajax request object XmlHttp the HA!

Ha ha, ha us to simulate, it is the original teacher training to make students write the code, and now allow others to criticize U.S. Ha!

Do not know how to say, or download code to see everyone, the implementation of it ha!

js version: ie directly to the Executive:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>

</body>
<script>
    function XmlHttp() {
        this.readyState = 0;
        this.serverStatus = -1;

    }
    XmlHttp.prototype.onreadyStatechange = function() {
    };
    XmlHttp.prototype.open = function(method, url, asynchronous) {
        while (true) {
            var vNum = Math.random();
            vNum = Math.round(vNum * 2);
            var senType = "异步";
            if (!asynchronous) {
                senType = "同步";
            }
            alert(senType + "方式到服务器" + url + "  " + method + "请求");
            if (vNum == 0) {
                this.serverStatus = 500;
                this.onreadyStatechange()
                break;
            }
            if (vNum == 1) {
                this.serverStatus = 404;
                this.onreadyStatechange()
                break;
            }
            if (vNum == 2) {
                this.serverStatus = 200;
                this.onreadyStatechange()
                break;
            }
        }
    };


    var myXmlHttp = new XmlHttp();
    myXmlHttp.onreadyStatechange = myCallBack;
    myXmlHttp.open(get, "www.163.com", true);
    function myCallBack() {
        if (myXmlHttp.serverStatus == 500) {
            alert(errcode=500 服务器不可用);
        }
        if (myXmlHttp.serverStatus == 404) {
            alert(errcode=404 URL资源不存在);
        }
        if (myXmlHttp.serverStatus == 200) {
            alert( areturn success);
        }
    }
</script>
</html>

java版本:

核心类介绍:

/*annoation用来标注客户端的回调方法*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AjaxCallBack {
}

/*回调的实现处理类*/

package com.bobo.mockajaxcallback.r5;

import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: xingbo.xu
 * Date: 2007-11-10
 * Time: 19:28:57
 * QQ群:77082582
 */
public class CallBackClient implements Runnable {
    private String onreadyStateChage;

    public CallBackClient(String onreadyStateChage) {
        this.onreadyStateChage = onreadyStateChage;
    }

    public void run() {
        doClientMethod();
    }

    private void doClientMethod() {
        //   String classPath = onreadyStateChage.substring(0, onreadyStateChage.indexOf("|"));
        //      String methodName = onreadyStateChage.substring(onreadyStateChage.indexOf("|") + 1);
        //  System.out.println(classPath);
        //  System.out.println(methodName);
        String classPath = onreadyStateChage;
        try {
            Class clazz = Class.forName(classPath);
            Method[] methods = clazz.getDeclaredMethods();
            Method ajaxCallBckMethod = null;
            for (Method method : methods) {
                if (method.isAnnotationPresent(AjaxCallBack.class)) {
                    ajaxCallBckMethod = method;
                }
            }
            if (ajaxCallBckMethod != null) {
                ajaxCallBckMethod.invoke(clazz.newInstance());
            } else {
                System.out.println("The class -->" + onreadyStateChage + " no @AjaxCallBack");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/*mock ajax core object */

package com.bobo.mockajaxcallback.r5;

 

/**
 * Created by IntelliJ IDEA.
 * User: xingbo.xu
 * Date: 2007-11-10
 * Time: 19:28:57
 * QQ群:77082582
 */
public class XMLHttpRequest {
    /**
     * 回调指针
     */
    public String onreadyStateChage;
    public int readyState;
    public int status;
    public String responseTest;
    public CallBackClient client;
    private boolean asynchronous;

    private XMLHttpRequest() {
    }

    public static XMLHttpRequest createXMLHttpRequest() {
        return new XMLHttpRequest();
    }

    /**
     * @param method       get or post
     * @param url
     * @param asynchronous true
     */

    public void open(String method, String url, boolean asynchronous) {
        this.asynchronous = asynchronous;


        sleep(2000);
        if (onreadyStateChage == null || onreadyStateChage.length() < 1) {
            setReadyState(0);
            System.out.println("没有初始化");
            return;
        }


        setReadyState(1);
        sleep(2000);
        System.out.println("正在加载");

        setReadyState(2);
        sleep(2000);
        System.out.println("已加载");

        setReadyState(3);
        sleep(2000);
        System.out.println("交互中");

        sleep(2000);
        responseTest = "hello Ajax";
        status = 200;
        setReadyState(4);
        System.out.println("完成");
        if (!asynchronous) {
            doClient();
        }

    }


    public void setReadyState(int readyState) {
        //    System.out.println("setReadyState");
        this.readyState = readyState;
        if (asynchronous) {
            doClient();
        }
    }

    private void doClient() {
        if (client == null)
            client = new CallBackClient(onreadyStateChage);

        Thread t = new Thread(client);
        t.start();
    }

    private  void sleep(int mSecond) {
        try {
            Thread.currentThread().sleep(mSecond);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

/*客户端测试类*/

package com.bobo.mockajaxcallback.r5;


/**
 * Created by IntelliJ IDEA.
 * User: xingbo.xu
 * Date: 2007-11-10
 * Time: 19:28:57
 * QQ群:77082582
 */
public class Client {
    static XMLHttpRequest xmlHttpRequest;

    public static void main(String[] args) {
        xmlHttpRequest = XMLHttpRequest.createXMLHttpRequest();

        xmlHttpRequest.onreadyStateChage = "com.bobo.mockajaxcallback.r5.Client";
        xmlHttpRequest.open("", "", true);
        System.out.println("-----------");
        xmlHttpRequest.open("", "", false);

    }

    @AjaxCallBack
    public  void clientMethod() {
        //   System.out.println("clientMethod");
        if (xmlHttpRequest.readyState == 0) {
            System.out.println("client 0");
        }
        if (xmlHttpRequest.readyState == 1) {
            System.out.println("client 1");
        }
        if (xmlHttpRequest.readyState == 2) {
            System.out.println("client 2");
        }
        if (xmlHttpRequest.readyState == 3) {
            System.out.println("client 3");
        }
        if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
            System.out.println(xmlHttpRequest.responseTest);
        }


    }
}

 Original Source:
http://www.codeweblog.com/ajax-and-js-quest-analog-java-implementation/

AddThis Social Bookmark Button

Posted at 09:10:46 am | Permalink | Posted in Java  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange