Java 用POST方式 传对象给 Servlet

转自:http://blog.csdn.net/a9529lty/article/details/6454156

Java代码

[java]  view plain  copy
  1. package dbConn;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import java.io.ObjectOutputStream;  
  6.   
  7. import java.io.OutputStream;  
  8.   
  9. import java.net.HttpURLConnection;  
  10.   
  11. import java.net.MalformedURLException;  
  12.   
  13. import java.net.URL;  
  14.   
  15. import bean.Tick;  
  16.   
  17. import flex.messaging.util.URLEncoder;  
  18.   
  19. public class TestAction {  
  20.   
  21. public static void main(String[] args) throws Exception{  
  22.   
  23. TestAction test = new TestAction();  
  24.   
  25. test.test();  
  26.   
  27. }  
  28.   
  29. public void test() throws Exception {  
  30.   
  31. URL url = new URL("http://localhost:8080/HRC/servlet/DataPushServlet");  
  32.   
  33. HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
  34.   
  35. urlConn.setDoOutput(true);  
  36.   
  37. urlConn.setDoInput(true);  
  38.   
  39. urlConn.setUseCaches(false);  
  40.   
  41. urlConn.setRequestProperty("Content-type","application/x-java-serialized-object");  
  42.   
  43. urlConn.setRequestMethod("POST");  
  44.   
  45. urlConn.connect();  
  46.   
  47. OutputStream outStrm = urlConn.getOutputStream();  
  48.   
  49. ObjectOutputStream oos = new ObjectOutputStream(outStrm);  
  50.   
  51. Tick tick = new Tick();  
  52.   
  53. oos.writeObject(tick);  
  54.   
  55. oos.flush();  
  56.   
  57. oos.close();  
  58.   
  59. InputStream inStrm = urlConn.getInputStream();  
  60.   
  61. }  
  62.   
  63. }  

 

Java代码

[java]  view plain  copy
  1. package servlet;  
  2.   
  3.   import java.io.BufferedReader;  
  4.   
  5.   import java.io.IOException;  
  6.   
  7.   import java.io.InputStream;  
  8.   
  9.   import java.io.InputStreamReader;  
  10.   
  11.   import java.io.ObjectInputStream;  
  12.   
  13.   import java.io.ObjectOutputStream;  
  14.   
  15.   import java.io.OutputStream;  
  16.   
  17.   import java.io.PrintWriter;  
  18.   
  19.   import java.math.BigDecimal;  
  20.   
  21.   import java.util.Date;  
  22.   
  23.   import javax.servlet.ServletException;  
  24.   
  25.   import javax.servlet.http.HttpServlet;  
  26.   
  27.   import javax.servlet.http.HttpServletRequest;  
  28.   
  29.   import javax.servlet.http.HttpServletResponse;  
  30.   
  31.   import bean.Tick;  
  32.   
  33.   import flex.messaging.MessageBroker;  
  34.   
  35.   import flex.messaging.messages.AsyncMessage;  
  36.   
  37.   import flex.messaging.util.UUIDUtils;  
  38.   
  39.   public class DataPushServlet extends HttpServlet {  
  40.   
  41.   public DataPushServlet() {  
  42.   
  43.   super();  
  44.   
  45.   }  
  46.   
  47.   public void destroy() {  
  48.   
  49.   super.destroy(); // Just puts "destroy" string in log  
  50.   
  51.   // Put your code here  
  52.   
  53.   }  
  54.   
  55. /** 
  56.  
  57.   * Initialization of the servlet. <br> 
  58.  
  59.   * 
  60.  
  61.   * @throws ServletException if an error occurs 
  62.  
  63.   */  
  64.   
  65.   public void init() throws ServletException {  
  66.   
  67.   // Put your code here  
  68.   
  69.   }  
  70.   
  71.   public void doGet(HttpServletRequest request, HttpServletResponse response)  
  72.   
  73.   throws ServletException, IOException {  
  74.   
  75.   //System.out.println("doGet");  
  76.   
  77.   doPost(request,response);  
  78.   
  79.   }  
  80.   
  81.   public void doPost(HttpServletRequest request, HttpServletResponse response)  
  82.   
  83.   throws ServletException, IOException {  
  84.   
  85.   System.out.println("doPost");  
  86.   
  87.   InputStream in = request.getInputStream();  
  88.   
  89.   ObjectInputStream ois = new ObjectInputStream(in);  
  90.   
  91.   try{  
  92.   
  93.   Tick tick = (Tick)(ois.readObject());  
  94.   
  95.   System.out.println(tick.getAskPrice());  
  96.   
  97.   System.out.println(tick.getBidPrice());  
  98.   
  99.   System.out.println(tick.getMidPrice());  
  100.   
  101.   }catch(Exception e){}  
  102.   
  103.   }  
  104.   
  105.   public void push(String para){  
  106.   
  107.   MessageBroker msgBroker = MessageBroker.getMessageBroker(null);  
  108.   
  109.   String clientID = UUIDUtils.createUUID();  
  110.   
  111.   //        Tick tick = new Tick();  
  112.   
  113.   //        tick.setAskPrice(new BigDecimal("100"));  
  114.   
  115.   //        tick.setBidPrice(new BigDecimal("100"));  
  116.   
  117.   //        tick.setMidPrice(new BigDecimal("100"));  
  118.   
  119.   //        tick.setTickTime(new Date());  
  120.   
  121.   //  
  122.   
  123.   //        tick.setSeqno(name);  
  124.   
  125.   AsyncMessage msg = new AsyncMessage();  
  126.   
  127.   msg.setDestination("tick-data-feed");  
  128.   
  129.   msg.setHeader("DSSubtopic""tick");  
  130.   
  131.   msg.setClientId(clientID);  
  132.   
  133.   msg.setMessageId(UUIDUtils.createUUID());  
  134.   
  135.   msg.setTimestamp(System.currentTimeMillis());  
  136.   
  137.   msg.setBody(para);  
  138.   
  139.   msgBroker.routeMessageToService(msg, null);  
  140.   
  141.   }  
  142.   
  143.   }  

 

Java代码

[java]  view plain  copy
  1. package bean;  
  2.   
  3.   import java.io.Serializable;  
  4.   
  5.   import java.math.BigDecimal;  
  6.   
  7.   import java.util.Date;  
  8.   
  9.   public class Tick implements Serializable {  
  10.   
  11.   private String askPrice = "aaaaaaaa";  
  12.   
  13.   private String bidPrice = "bbbbbbbb";  
  14.   
  15.   private String midPrice = "cccccccc";  
  16.   
  17.   ..........  
  18.   
  19.   }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值