歡迎您光臨本站 註冊首頁

Java用POST傳對象給Servlet

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  為了實現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

  }

  /**

  * 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";

  ..........

  }


[火星人 ] Java用POST傳對象給Servlet已經有774次圍觀

http://coctec.com/docs/java/show-post-60219.html