Servlet与Client之间的http协议通讯

        servlet其实只是作为一个服务端存在,替代了单独写socket实现的server.网上的命名叫"隧道技术",其实不过就是通过http协议,穿墙.真要屏蔽,也不是不可能.

      以下为实现代码

      web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" 
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7.   <servlet>
  8.     <servlet-name>basic</servlet-name>
  9.     <servlet-class>communication.BasicServlet</servlet-class>
  10.   </servlet>
  11.   <servlet-mapping>
  12.     <servlet-name>basic</servlet-name>
  13.     <url-pattern>/basic.do</url-pattern>
  14.   </servlet-mapping>
  15. </web-app>

    服务器端    

  1. package communication;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletInputStream;
  9. import javax.servlet.ServletOutputStream;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. /**
  14.  * @author Shengyan
  15.  * @version 创建时间:2008-8-14 下午01:13:46
  16.  * 类说明:
  17.  */
  18. public class BasicServlet extends HttpServlet {
  19.     @Override
  20.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  21.             throws ServletException, IOException {
  22.         
  23.         // read
  24.         ServletInputStream sis = request.getInputStream();
  25.         ObjectInputStream ois = new ObjectInputStream(sis);
  26.         try {
  27.             Message readMsg = (Message) ois.readObject();
  28.         } catch (ClassNotFoundException e) {
  29.             e.printStackTrace();
  30.         }
  31.         ois.close();
  32.         
  33.         // write
  34.         ServletOutputStream sos = response.getOutputStream();
  35.         ObjectOutputStream oos = new ObjectOutputStream(sos);
  36.         Message writeMsg = new Message();
  37.         oos.writeObject(writeMsg);
  38.         oos.flush();
  39.         oos.close();
  40.     }
  41.     @Override
  42.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  43.             throws ServletException, IOException {
  44.         doGet(request, response);
  45.     }
  46. }

    客户端

  1. package communication;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. /**
  9.  * @author Shengyan
  10.  * @version 创建时间:2008-8-18 下午03:23:44
  11.  * 类说明:
  12.  */
  13. public class BasicClient {
  14.     
  15.     private HttpURLConnection connection;
  16.     
  17.     public void connect() {
  18.         String server = "http://localhost";
  19.         String port = "8080";
  20.         String path = "/javaComm/basic.do";
  21.         String timeout ="6000";
  22.         String contentType = "text/html" // 网速快时使用该类型
  23. //      String contentType = "application/octet - stream" // 网速慢时使用该类型
  24.         try {
  25.             URL url = new URL(server + ":" + port + path);
  26.             connection = (HttpURLConnection) url.openConnection();
  27.             connection.setDoInput(true);
  28.             connection.setDoOutput(true);
  29.             connection.setConnectTimeout(Integer.parseInt(timeout));
  30.             connection.setRequestMethod("POST");// Method
  31.             connection.setRequestProperty("Content-type", contentType);
  32.     //      connection.setRequestProperty("Connection", "close");
  33.     //      connection.setRequestProperty("Content-Length",xxx);
  34.         } catch (MalformedURLException e) {
  35.             e.printStackTrace();
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.     
  41.     public Message readMessage() {
  42.         Message vo = null;
  43.         try {
  44.             ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
  45.             try {
  46.                 vo = (Message) ois.readObject();
  47.             } catch (ClassNotFoundException e) {
  48.                 e.printStackTrace();
  49.             }
  50.             ois.close();
  51.         } catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.         return vo;
  55.     }
  56.     
  57.     public void writeMessage(Message cv) {
  58.         try {
  59.             ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
  60.             oos.writeObject(cv);
  61.             oos.flush();
  62.             oos.close();
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         }
  66.             
  67.     }
  68.     
  69.     public int getCode() {
  70.         try { 
  71.             int code = connection.getResponseCode();
  72.             System.out.println("code     " + code);
  73.             return code;
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.             return 0;
  77.         }
  78.     }
  79.     
  80.     public static void main(String... args) throws IOException {
  81.         BasicClient client = new BasicClient();
  82.         client.connect();
  83.         client.writeMessage(new Message());
  84.         Message msg = client.readMessage();
  85.     }
  86. }
  87.     
  88. 通讯对象 必须实现可序列化,才可以传送对象
    1. import java.io.Serializable;
    2. public class Message implements  implements Serializable {
    3.     private String message;
    4.     public getMessage(String message) {
    5.         return this.message
    6.     }
    7.     public setMessage(String message) {
    8.         this.message = message;
    9.     }
    10. }
    11.     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值