Java用POST传对象给Servlet

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

搜了很久,也没找到Java类里调用Servlet的例子,只好用Post方法传对象。

发送POST的例子在 : http://lodachi.javaeye.com/?show_full=true

Java代码

  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. }  
  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代码

  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.   }  
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代码

  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.   }  
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";  ..........  }

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hfrujhv/article/details/84095035