当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java用POST传对象给Servlet
发布时间:2010/7/16 10:20:17 来源:城市学习网 编辑:ziteng
  为了实现BlazeDS的实时推送功能,找到了个用Servlet实现的例子。但是例子采用的是线程每秒推送,这样有些浪费资源。
  搜了很久,也没找到Java类里调用Servlet的例子,只好用Post方法传对象。
  发送POST的例子在 : http://lodachi.javaeye.com/?show_full=true
  Java代码
  package dbConn;
  import java.io.InputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import bean.Tick;
  import flex.messaging.util.URLEncoder;
  public class TestAction {
  public static void main(String[] args) throws Exception{
  TestAction test = new TestAction();
  test.test();
  }
  public void test() throws Exception {
  URL url = new URL("http://localhost:8080/HRC/servlet/DataPushServlet");
  HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
  urlConn.setDoOutput(true);
  urlConn.setDoInput(true);
  urlConn.setUseCaches(false);
  urlConn.setRequestProperty("Content-type","application/x-java-serialized-object");
  urlConn.setRequestMethod("POST");
  urlConn.connect();
  OutputStream outStrm = urlConn.getOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(outStrm);
  Tick tick  = new Tick();
  oos.writeObject(tick);
  oos.flush();
  oos.close();
  InputStream inStrm = urlConn.getInputStream();
  }
  }
  Java代码
  package servlet;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.math.BigDecimal;
  import java.util.Date;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import bean.Tick;
  import flex.messaging.MessageBroker;
  import flex.messaging.messages.AsyncMessage;
  import flex.messaging.util.UUIDUtils;
  public class DataPushServlet extends HttpServlet {
  public DataPushServlet() {
  super();
  }
  public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
  } [NextPage]   /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
  public void init() throws ServletException {
  // Put your code here
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  //System.out.println("doGet");
  doPost(request,response);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  System.out.println("doPost");
  InputStream in = request.getInputStream();
  ObjectInputStream ois = new ObjectInputStream(in);
  try{
  Tick tick = (Tick)(ois.readObject());
  System.out.println(tick.getAskPrice());
  System.out.println(tick.getBidPrice());
  System.out.println(tick.getMidPrice());
  }catch(Exception e){}
  }
  public void push(String para){
  MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
  String clientID = UUIDUtils.createUUID();
  //        Tick tick = new Tick();
  //        tick.setAskPrice(new BigDecimal("100"));
  //        tick.setBidPrice(new BigDecimal("100"));
  //        tick.setMidPrice(new BigDecimal("100"));
  //        tick.setTickTime(new Date());
  //
  //        tick.setSeqno(name);
  AsyncMessage msg = new AsyncMessage();
  msg.setDestination("tick-data-feed");
  msg.setHeader("DSSubtopic", "tick");
  msg.setClientId(clientID);
  msg.setMessageId(UUIDUtils.createUUID());
  msg.setTimestamp(System.currentTimeMillis());
  msg.setBody(para);
  msgBroker.routeMessageToService(msg, null);
  }
  }
  Java代码
  package bean;
  import java.io.Serializable;
  import java.math.BigDecimal;
  import java.util.Date;
  public class Tick implements Serializable {
  private String askPrice = "aaaaaaaa";
  private String bidPrice = "bbbbbbbb";
  private String midPrice = "cccccccc";
  ..........
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved