3.在web.xml中加入DWR使用能力: javascript
<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> <init-param> <param-name> crossDomainSessionSecurity</param-name > <param-value> false</param-value > </init-param> <init-param> <param-name> allowScriptTagRemoting</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>
package sugar.dwr; public class MessagePush { public void send(String str){ System. out.println(str); } }
<?xml version="1.0" encoding= "UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd" > <dwr> <allow> <create creator="new" javascript= "messagePush"> <param name="class" >sugar.dwr.MessagePush</ param> </create> </allow> </dwr>
<%@ page language= "java" import ="java.util.*" pageEncoding="UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head > <title >DWR</ title> <script type= "text/javascript" src ="js/jquery-1.8.3.js"></ script> <script type= "text/javascript" src ="dwr/util.js"></ script> <script type="text/javascript" src= "dwr/engine.js"></script > <script type="text/javascript" src= "dwr/interface/messagePush.js" ></script> </head > <body > <table border="0" > <tr> <td>< input id ="content" type="text" /></td> <td>< input id ="send" type="button" value= "send"/></td > </tr> </table> <script type="text/javascript" > $( "#send").click(function(){ var content = $("#content" ).val(); messagePush.send(content); }); </script> </body > </html>
測試結果: html