Web server push using the DWR framework

Source: Internet
Author: User

Java-based mature server push frameworks include DWR.

DWR is an open-source solution using the Apache license protocol. It contains the server-side Java library, a DWR
Servlet and JavaScript library. Although DWR is not the only available Ajax-RPC toolkit on the Java platform, it is the most mature and provides many useful functions. From the simplest point of view, DWR is an engine that can expose methods of server-side Java objects to JavaScript code. Using DWR can effectively eliminate all Ajax request-response loops from application code. This means that the client Code no longer needs to directly process the XMLHTTPRequest object or server response. You no longer need to compile Object serialization code or use a third-party tool to convert an object into XML. You do not even need to write servlet code to adjust Ajax requests to call Java domain objects.

DWR has added the push function since 2.0, that is, it can send data from the web-server to the browser during asynchronous transmission.

A simple DWR push Program

Step 1 import jar packages related to DWR to the Project

Step 2 configure the Web. xml file

<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet-Name> DWR-invoker </servlet-Name> <servlet-class> Org. directwebremoting. servlet. dwrservlet </servlet-class> <! -- Set whether DWR push technology is allowed --> <init-param> <param-Name> activereverseajaxenabled </param-Name> <param-value> true </param-value> </init-param> <param-Name> maxwaitafterwrite </param-Name> <param-value>-1 </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> </Web-app>

Step 3: Compile the com. Im. Service. sendpushservice class

Public class sendpushservice {// send the message public void send (string MSG) {system. out. println ("=========== the send method is called ========="); scriptbuffer = new scriptbuffer (); // construct the JS script webcontext = webcontextfactory. get (); scriptsession myscsession = webcontext. getscriptsession (); scriptbuffer. appendscript ("dwrtest ("); scriptbuffer. appenddata (MSG); scriptbuffer. appendscript (")"); util = new util (myscsession); util. addscript (scriptbuffer); // push messages to the client }}

Step 4 define the externally exposed interface in the dwr. xml file

<allow><create creator="new" javascript="SendPushService"><param name="class" value="com.im.service.SendPushService"/></create></allow>

Step 5: compile a JSP file.

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

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.