Web Quotation system based on Pushlet implementation

Source: Internet
Author: User

The reasons for the project need to implement web-based online quotation inquiry system. User demand is very simple is the customer through the website, the product can be submitted to buy and sell prices, the system has a centralized display screen to show the 10 best Buy and sell prices. There are two ways to implement the above features:

Push Technology:

A mechanism based on a server that proactively sends information to the client's technology. The advantage of push technology is the initiative and timeliness of information.

Pull technology:

The client actively requests information, using the client to refresh the page periodically, can be implemented using AJAX technology, automatically reload every n seconds, if the server-side data changes during this period, the client can obtain new content.

According to our needs, the use of push technology to achieve the above features is a better choice. After technical selection, the final choice is to use the Pushlet framework. Pushlet uses the Observer pattern, the client subscriber is interested in the event, the server side assigns each client a session ID as a token, the event source will be the new generation of the Russian event multicast sent to the subscriber's event queue. The specific principle here does not introduce too much, this article mainly introduces the use of Pushlet to achieve a calculation of the average price of the demo.

Create a new Quoteservlet. Class, a servlet used to implement a quote from the client to submit a user and trigger a price change event on the server side.

Package Com;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/* * Servlet method to implement data submission */public class Quoteservlet extends Httpservlet{private Static final Long Serialversionuid = 2386052823761867369l;public static Double Ttprice = 0.00;public void Doget (Httpservle Trequest request, HttpServletResponse response) throws Servletexception, ioexception{//call HttpServlet The self-contained log function outputs information to the console This.log ("Execute Doget Method ... ");//Handle Dopostthis.execute (request, response);//Call Pushlet update each other's data string valuestring = Double.tostring (Ttprice); Quotepushlet.sendbymulticast (valuestring);} /** * Executes this function when the page is accessed by POST. */public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { This.log ("Execute DoPost Method ... ");//processing Dopostthis.execute (request, response);//Call Pushlet update each other's data string valuestring = Double.tostring (ttprice);//Directed Broadcast//quotepushlet.sendbymulticast (valuestring);//directed sending Quotepushlet.sendbyunicast (valuestring);// Quotepushlet.sendbybroadcast (valuestring);} private void Execute (httpservletrequest request, httpservletresponse response) throws Servletexception, ioexception{ String pricestring = Getreqstr (Request.getparameter ("Qprice"), true);D ouble pricedb = 0.0;if (pricestring! = null && Amp  pricestring = "") {Pricedb = double.valueof (pricestring);} if (ttprice! = 0.00) {Ttprice = Ttprice + Pricedb;ttprice = TTPRICE/2;}    Else{ttprice = Pricedb;}}    Private String getreqstr (Object s,boolean isgetmethod) {if (s==null) return "";    if (!isgetmethod) return ((String) s). ToString ();    try{return New String (((String) s). ToString (). GetBytes ("Iso8859_1"), "GBK");    }catch (Exception e) {return ""; }    }}

Create a Quotepushlet class that implements event broadcasts. My side uses Pushlet's unicast,multicast,broadcast to send broadcast events, respectively. Where unicast is a directed hair, multicast,broadcast are all global broadcasts.

Package Com;import Java.util.hashmap;import Java.util.map;import nl.justobjects.pushlet.core.dispatcher;import Nl.justobjects.pushlet.core.event;import Nl.justobjects.pushlet.core.session;import nl.justobjects.pushlet.core.sessionmanager;/* * Use Push's Broadcast,multicast,unicast method * * To achieve the presentation of data */public class Quotepushlet {public static void Sendbymulticast (String value) {map<string, string> Map = new hashmap<string, Str Ing> (); Map.put ("Avgprice", value); Event event = Null;event = Event.createdataevent ("/quote/newprice", map);//mass broadcast dispatcher.getinstance (). Multicast ( event);} public static void Sendbyunicast (String value) {map<string, string> Map = new hashmap<string, string> (); Map.put ("Avgprice", value); Event event = Null;event = Event.createdataevent ("/quote/newprice", map); Session[] Sessions = Sessionmanager.getinstance (). Getsessions (); for (Session session:sessions) {// Directed send Dispatcher.getinstance (). Unicast (event, Session.getid ());}} public static void SENDBYBROADCASt (String value) {map<string, string> Map = new hashmap<string, string> (); Map.put ("Avgprice", value); Event event = Null;event = Event.createdataevent ("/quote/newprice", map);D ispatcher.getinstance (). Broadcast (event);}}
The use of Pushlet requires configuration in Web. Xml.

<?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/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><!--Define the Pushlet servlet--><servlet>< servlet-name>pushlet</servlet-name><servlet-class>nl.justobjects.pushlet.servlet.pushlet</ servlet-class><load-on-startup>3</load-on-startup></servlet><!--Define the servlet Mappings. --><!--the Pushlet--><servlet-mapping><servlet-name>pushlet</servlet-name>< Url-pattern>/pushlet.srv</url-pattern></servlet-mapping><servlet><servlet-name> Quoteservlet</servlet-name><servlet-class>com. quoteservlet</servlet-class></servlet><servlet-mapping><servlet-name>quoteservlet</ Servlet-name><url-pattern>/quoteservlet</url-pattErn></servlet-mapping></web-app> 

Also need pushlet.properties and sources.properties, here does not watch.

The JSP page code for the quote is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The front page of the price show is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>



Web Quotation system based on Pushlet implementation

Related Article

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.