At the weekend, study dwr at home and do an example. Implements a message from the client to the server, after the message is returned, displayed on the client. The code is as follows:
JSP page:
<%@ page language= "java" pageencoding= "Utf-8"%>
<script src= ' dwr/interface/hello.js ' ></script>
<script src= ' dwr/engine.js ' ></script>
<script src= ' dwr/util.js ' ></script>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<base href= "<%=basePath%>" >
<title> This is the test dwr</title>
<script type= "Text/javascript" >
function Tijiao ()
{
var info=document.all["Infor"].value;
Hello.say (Info,callback);
}
function callback (data)
{
Document.all["Con"].innerhtml=data;
}
</script>
<body>
<br><br><br><br><br><br><br>
<center>
Haha, see the address is not AH. The basepath of this page is: <%=basePath%><br>
Haha, see the address is not AH. The path to this page is: <%=path%><br><br><br><br>
<input type= "text" name= "infor" >
<input type= "button" value= "Look at Me" onclick= "Tijiao ()" ><br>
The content returned after submission is as follows:
<br><font color= "Red" > <span id= "con" ></span> </font>
</center>
</body>
server-side Java classes
package;
public class Hello {
public string Say (String infor) {
Return "The message you sent is:" + infor;
}
}
dwr.xml File:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE dwr Public "-//getahead limited//dtd Direct Web Remoting 2.0//en" "Http://getahead.org/dwr/dwr20.dtd" >
<dwr>
<allow>
<create creator= "new" javascript= "Hello" scope= "session" >
<param name= "Class" value= ". Hello "/>
<include method= "Say"/>
</create>
</allow>
</dwr>
web.xml File:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<description></description>
<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-app>
Jar Pack yourself added, note I lazy write, make up to see it!