Pushlet server push

Source: Internet
Author: User

--------------------- Add ------------------------ to the JSP page ----------------------

 

<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<Base href = "<% = basepath %>">
// Download from the official website
<SCRIPT type = "text/JavaScript" src = "<% = PATH %>/CC/util/JS/ajax-pushlet-client.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">

Pl. _ Init ();

// Listen to the mutisend time. mutisend is defined by me.
Pl. joinlisten ('/mutisend ');
Function ondata (event ){
Try {

// Read content
VaR context = event. Get ("MSG ");

// Solve Chinese Character garbled characters
Context = decodeuricomponent (context );
If (null! = Context & "undefined "! = Context ){
// Var A = Document. getelementbyid ("contex ");
// A. innerhtml = context;
Pushinfoshowdiv (context );
}
} Catch (e ){
}
// Exit
// Pl. Leave ();
}
</SCRIPT>


 

 

-------------------------- Add to the Web. xml file

  <!-- pushlet -->  <servlet><servlet-name>pushlet</servlet-name><servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class><load-on-startup>3</load-on-startup></servlet><servlet-mapping><servlet-name>pushlet</servlet-name><url-pattern>/pushlet.srv</url-pattern></servlet-mapping>


 

------------------------ Java file -----------------

// Push public void mutisend () {try {// obtain the data list <remindinfo> List = retrieve (null, null, null, null, null); string remindcontent = ""; if (null! = List & 0! = List. size () {remindcontent = List. get (0 ). getremindcontent ();} event = event. createdataevent ("/mutisend"); event. setfield ("MSG", urlencoder. encode (remindcontent + "<br/> push time:" + new date (). tolocalestring (), "UTF-8"); // fix garbled dispatcher. getinstance (). multicast (event);} catch (unsupportedencodingexception e) {// todo auto-generated catch blocke. printstacktrace ();}}


If you need to click send on the page and then push it to each client, add

Event event = event. createdataevent ("/mutisend ");

Event. setfield ("MSG", urlencoder. encode (remindcontent + "<br/> push time:" + new date (). tolocalestring (), "UTF-8"); // fix garbled

Dispatcher. getinstance (). multicast (event );

You can use these three codes.

 

------------ Effect -------------

 

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.