DWR for page Push Simple instance

Source: Internet
Author: User

Implementation function: Sendmsg.jsp sends a message to the background, the background pushes the message to the Showmsg.jsp page.

1, Web. XML configuration, add the following content in Web. xml:

<!--using DWR for page message push  --><servlet><display-name>dwr servlet</display-name>< Servlet-name>dwr-invoker</servlet-name><servlet-class>org.directwebremoting.servlet.dwrservlet </servlet-class><init-param><param-name>debug</param-name><param-value>true</ param-value></init-param><!--the way to use polling and comet--><init-param><param-name> pollandcometenabled</param-name><param-value>true</param-value></init-param>< Load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name> Dwr-invoker</servlet-name><url-pattern>/dwr/*</url-pattern></servlet-mapping>

2, Configuration Dwr.xml

<! DOCTYPE dwr public    "-//getahead limited//dtd Direct Web Remoting 3.0//en"    "HTTP://GETAHEAD.ORG/DWR/DWR30.DTD" ><dwr>  <allow>  <!--The JavaScript value here is the name of the third JS file referenced by the page--    <create creator= "new" javascript= "sendmsg" >      <param Name= "class" >iwco.dwrs.SendMsg</param>      </create>  </allow></dwr>

3. Java class Sendmsg.java

Package Iwco.dwrs;import Java.util.collection;import Org.directwebremoting.scriptbuffer;import Org.directwebremoting.scriptsession;import Org.directwebremoting.webcontext;import Org.directwebremoting.webcontextfactory;import Org.directwebremoting.proxy.dwr.util;public class SendMsg {@ Suppresswarnings ("deprecation") public void sendmsg (String msg) {//get context webcontext Contex = Webconte                    Xtfactory.get ();          Get the page you want to push to DWR3 for the project name, be sure to add it.                  collection<scriptsession> sessions = Contex.getscriptsessionsbypage ("/dwrtest/showmsg.jsp");                  Do not know how to explain this, Util Util = new Util (sessions);           The following is the creation of a JavaScript script, which is equivalent to adding a sentence show (msg) to the page script;          Scriptbuffer sb = new Scriptbuffer ();          Sb.appendscript ("Show (");          Sb.appenddata (msg);                    Sb.appendscript (")");      Push Util.addscript (SB); }  }

4. Page

sendmsg.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
showmsg.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

DWR for page Push Simple instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.