Real-time stock information updates with JSON

Source: Internet
Author: User

Real-time update of stock information using JSON one, server code 1, Stock.java
package bean;public class Stock {    private String code;    private String name;    private int price;    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getPrice() {        return price;    }    public void setPrice(int price) {        this.price = price;    }}
2, Actionservlet.java
Package Web;import Java.io.ioexception;import java.io.printwriter;import java.util.arraylist;import java.util.List; Import Java.util.random;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Bean. Stock;import Net.sf.json.jsonarray;public class Actionservlet extends httpservlet{public void service (httpservletrequ        EST request,httpservletresponse response) throws servletexception,ioexception{System.out.println ("service ()");        Gets the request path String Uri=request.getrequesturi ();        System.out.println (URI);        Parse Request path String action=uri.substring (Uri.lastindexof ("/"), Uri.lastindexof ("."));        System.out.println (action);        Response.setcontenttype ("Text/html;charset=utf-8");        PrintWriter Out=response.getwriter (); if ("/quoto". Equals (Action)) {//simulation generates several stock information list<stock> stocks=new ARRAYLIST&LT;STOCK> ();            Random r=new random ();                for (int i=0;i<8;i++) {stock s=new stock ();                S.setcode ("600877" +r.nextint (10));                S.setname ("China jialing" +r.nextint (100));                S.setprice (10+r.nextint (1000));            Stocks.add (s);            The parameters of the//fromobject method can be either porcine or combined with Jsonarray jsonarr=jsonarray.fromobject (stocks);            String jsonstr=jsonarr.tostring ();            System.out.println (JSONSTR);        Out.println (JSONSTR); }    }}
3. Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "> <display-name>ajax-day02</display-name > <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm& Lt;/welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</ Welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</ welcome-file> </welcome-file-list> <servlet> <servlet-name>action</servlet-name> <se Rvlet-class>web. actionservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>action</ Servlet-name> <url-pattern>*.do</url-pattern> </servleT-mapping></web-app> 
4, Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>cn.qiuuuu</groupId>  <artifactId>ajax-day02</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <dependencies>      <dependency>          <groupId>net.sf.json-lib</groupId>          <artifactId>json-lib</artifactId>          <version>2.4</version>          <classifier>jdk15</classifier>    </dependency>  </dependencies></project>
Second, client code 1.stock.jsp
<%@ page 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" >
Third, the realization effect

Real-time stock information updates with JSON

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.