推送消息
web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name><servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
---------------------------------------------------------------
web-inf/dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="Viewcode">
<param name="class" value="com.servlet.viewcode"/>
</create>
</allow>
</dwr>
---------------------------------------------------------------
viewcode
public class viewcode{
public static List<String> codelist;
public static int index=0;
public void savecode(String code) {
if(null==codelist){
codelist=new ArrayList<String>();
}
codelist.add(code);
index=codelist.size()-1;
}
public String getvode(int index){
if(null==codelist){
codelist=new ArrayList<String>();
}
if(0<codelist.size()&&index<codelist.size()){
return codelist.get(index);
}else{
try {
Thread.sleep(5000);
getvode(index);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
public int getindex(){
return index;
}
}
---------------------------------------------------------------
viewcode.jsp
<%@ page contentType="text/html; charset=gb2312" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Ch11--DWR使用入门</title>
<script language="javascript" src='/pushobj/dwr/engine.js'></script>
<script language="javascript" src='/pushobj/dwr/util.js'></script>
<script language="javascript" src="/pushobj/dwr/interface/Viewcode.js"></script>
<script language="javascript" src='/pushobj/dwr/engine.js'></script>
<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript">
function doTest() {
var name = dwr.util.getValue("name");
Viewcode.savecode(name);
}
function doview() {
var name = dwr.util.getValue("hidenindex");
name=parseInt(name);
Viewcode.getvode(name,function(data){
if(undefined!=data&&""!=data){
$("#viewcode").val(data);
$("#hidenindex").val(name+1);
}
doview();
});
}
$(function(){
Viewcode.getindex(function(data){
if(undefined!=data&&""!=data){
$("#hidenindex").val(data);
}
});
doview();
});
</script>
</head>
<body>
<input type="text" name="name">
<input type="button" name="count" value="cont" onClick="doTest()">
<input type="text" id="viewcode">
<input type="hidden" name="hidenindex" id="hidenindex" value="0">
</body>
</html>