DWR message push

Source: Internet
Author: User

Tag: DWR push example


The JSP system sometimes has the function of sending messages in a group or pushing messages in a timely manner. If Ajax requests are used, setting the setTimeout constant request server will put a lot of pressure on the browser, in this case, we can use the DWR technology to push messages. DWR pushes messages in a timely manner based on its own mechanism and can receive messages without requiring itself to request the server. You can send messages between webpages, and the server can send messages directly like a client. For example, online communication and Dynamic Refresh of stock market data.

The following is a simple example to describe the configuration.

To send messages to another webpage:

The result is as follows: click "send" and the message is displayed on another webpage.


Code:

Add the following code to Web. xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"    xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><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>        <init-param>            <param-name> activeReverseAjaxEnabled</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>

Add DWR. XML in the same directory as Web. xml:

<?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>

Java code: the package name and the configuration in DWR. XML are consistent.

Package sugar. DWR; import Java. util. collection; import Org. directwebremoting. browser; import Org. directwebremoting. scriptbuffer; import Org. directwebremoting. scriptsession; public class messagepush {public void send (final string content) {runnable run = new runnable () {private scriptbuffer script = new scriptbuffer (); Public void run () {system. out. println (content); // set the JS to be called and the script parameter. appendcall ("show", content); // obtain all scriptsession collections <scriptsession> sessions = browser. gettargetsessions (); // traverses every scriptsession for (scriptsession: Sessions) {scriptsession. addscript (SCRIPT) ;}}; // execute the push browser. withallsessions (run );}}

Sending page: The js method name is consistent with the Java method name in the XML configuration.

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <% string a = "A"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Acceptance page:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Introduce the jar package:

The JS files on the client web page only need to be imported into jquery. Other JS files are encapsulated in DWR.

Download the jar package and JS:

Http://download.csdn.net/detail/u011250851/7888111


If you have any questions, please reply to me directly.

Zookeeper

DWR message push

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.