ajax and js Quest Analog java implementation
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 = " Asynchronous ";
if (!asynchronous) {
senType = " Synchronization ";
}
alert(senType + " The way to the server " + url + " " + method + " Request ");
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 The server is unavailable ');
}
if (myXmlHttp.serverStatus == 404) {
alert('errcode=404 URL Resource does not exist ');
}
if (myXmlHttp.serverStatus == 200) {
alert('return success');
}
}
</script>
</html> java Version :
Core classes describes :
/*annoation Used to mark the client's callback method */
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 {
} /* The callback implementation class */
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 Group :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 Group :77082582
*/
public class XMLHttpRequest {
/**
* Callback pointer
*/
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(" Not initialized ");
return;
}
setReadyState(1);
sleep(2000);
System.out.println(" Loading ");
setReadyState(2);
sleep(2000);
System.out.println(" Loaded ");
setReadyState(3);
sleep(2000);
System.out.println(" Interaction ");
sleep(2000);
responseTest = "hello Ajax";
status = 200;
setReadyState(4);
System.out.println(" Complete ");
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();
}
}
} /* The client test class */
package com.bobo.mockajaxcallback.r5;
/**
* Created by IntelliJ IDEA.
* User: xingbo.xu
* Date: 2007-11-10
* Time: 19:28:57
* QQ Group :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);
}
}
}
Related Posts of ajax and js Quest Analog java implementation
-
JDBC example of a long time do not have JDBC forgot
A back-up here to stay. The first: The second:
-
Process migration from tomcat to websphere changes
Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm& ...
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...
-
hibernate generic generic DAO
hibernate generic generic DAO
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and oth ...
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q
-
Great collection of java interview topics
1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract d ...













Leave a Reply