DWR包含两个主要的部分:允许JavaScript从WEB服务器上一个遵循了AJAX原则的Servlet中获取数据.另外一方面一个JavaScript库可以帮助网站开发人员轻松地利用获取的数据来动态改变网页的内容,DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码。这样WEB开发人员就可以在JavaScript里使用Java代码,就像它们是浏览器的本地代码(客户端代码)一样;但是Java代码运行在WEB服务器端而且可以自由访问WEB 服务器的资源。出于安全的理由,WEB开发者必须适当地配置哪些Java类可以安全的被外部使用。下面讲解一下具体配置步骤。
1、在工程中引入dwr.jar,之后修改配置web.xml文件,添加配置具体代码如下:
dwr-invoker
org.directwebremoting.servlet.DwrServlet
crossDomainSessionSecurity
false
allowScriptTagRemoting
true
classes
java.lang.Object
activeReverseAjaxEnabled
true
initApplicationScopeCreatorsAtStartup
true
maxWaitAfterWrite
3000
debug
true
logLevel
WARN
2、在web.xml统计目录下新增dwr.xml文件,具体内容如下:
dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
"http://getahead.org/dwr/dwr30.dtd">
MessagePush在页面的javascript中使用,com.yoodb.service.MessagePush实现了想要调用的方法,其中MessagePush.java对被推送页面开放的java类,Test.java是对推送页面开放的java类。在javascript中使用MessagePush.java类中实现的方法,即可在dwr中调用。
3、引入JavaScript文件,具体如下:
注意:
1)dwr.xml配置的javascript中engine.js和util.js是必须引入的文件。
2)在任何一个用户登录的时候,都需要将其userId或者其他唯一性标识放入session中,我放的是userId,这里就以userId为唯一性标识。
3)在载入想推送的页面时,需要onload一个我在MessagePush类中实现的方法,当然了,需要使用dwr调用
被推送html页面具体内容代码如下:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
">
DWR DEMO//通过该方法与后台交互,确保推送时能找到指定用户
function onPageLoad(){
var userId = '${userinfo.userId}';
MessagePush.onPageLoad(userId);
}
//推送信息
function showMessage(autoMessage){
alert(autoMessage);
}
This is my DWR DEOM page.
其中MessagePush.java文件中实现方法,如下:
public class MessagePush{
public void onPageLoad(String userId) {
ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
scriptSession.setAttribute(userId, userId);
DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil();
try {
dwrScriptSessionManagerUtil.init();
System.out.println("cacaca");
} catch (ServletException e) {
e.printStackTrace();
}
}
}
对于onPageLoad()方法中DwrScriptSessionManagerUtil类的实现,具体如下:
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.directwebremoting.Container;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;
import org.directwebremoting.servlet.DwrServlet;
public class DwrScriptSessionManagerUtil extends DwrServlet{
private static final long serialVersionUID = -7504612622407420071L;
public void init()throws ServletException {
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
ScriptSessionListener listener = new ScriptSessionListener() {
public void sessionCreated(ScriptSessionEvent ev) {
HttpSession session = WebContextFactory.get().getSession();
String userId =((User) session.getAttribute("userinfo")).getHumanid()+"";
System.out.println("a ScriptSession is created!");
ev.getSession().setAttribute("userId", userId);
}
public void sessionDestroyed(ScriptSessionEvent ev) {
System.out.println("a ScriptSession is distroyed");
}
};
manager.addScriptSessionListener(listener);
}
}
4、推送html页面具体内容代码如下:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
">
My JSP 'MyJsp.jsp' starting pagefunction testPush() {
var msg = document.getElementById("msgId").value;
TestPush.sendMessageAuto(msg,"www.yoodb.com");
}
id值:
其中TeshPush.java文件,具体内容如下:
public class TestPush{
public void sendMessageAuto(String userid, String message){
final String userId = userid;
final String autoMessage = message;
Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
public boolean match(ScriptSession session){
if (session.getAttribute("userId") == null)
return false;
else
return (session.getAttribute("userId")).equals(userId);
}
}, new Runnable(){
private ScriptBuffer script = new ScriptBuffer();
public void run(){
script.appendCall("showMessage", autoMessage);
Collection sessions = Browser
.getTargetSessions();
for (ScriptSession scriptSession : sessions){
scriptSession.addScript(script);
}
}
});
}
}