servlet其实只是作为一个服务端存在,替代了单独写socket实现的server.网上的命名叫"隧道技术",其实不过就是通过http协议,穿墙.真要屏蔽,也不是不可能.
以下为实现代码
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>basic</servlet-name>
- <servlet-class>communication.BasicServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>basic</servlet-name>
- <url-pattern>/basic.do</url-pattern>
- </servlet-mapping>
- </web-app>
服务器端
- package communication;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletException;
- import javax.servlet.ServletInputStream;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * @author Shengyan
- * @version 创建时间:2008-8-14 下午01:13:46
- * 类说明:
- */
- public class BasicServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // read
- ServletInputStream sis = request.getInputStream();
- ObjectInputStream ois = new ObjectInputStream(sis);
- try {
- Message readMsg = (Message) ois.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- ois.close();
- // write
- ServletOutputStream sos = response.getOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(sos);
- Message writeMsg = new Message();
- oos.writeObject(writeMsg);
- oos.flush();
- oos.close();
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
客户端
- package communication;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- /**
- * @author Shengyan
- * @version 创建时间:2008-8-18 下午03:23:44
- * 类说明:
- */
- public class BasicClient {
- private HttpURLConnection connection;
- public void connect() {
- String server = "http://localhost";
- String port = "8080";
- String path = "/javaComm/basic.do";
- String timeout ="6000";
- String contentType = "text/html" // 网速快时使用该类型
- // String contentType = "application/octet - stream" // 网速慢时使用该类型
- try {
- URL url = new URL(server + ":" + port + path);
- connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.setConnectTimeout(Integer.parseInt(timeout));
- connection.setRequestMethod("POST");// Method
- connection.setRequestProperty("Content-type", contentType);
- // connection.setRequestProperty("Connection", "close");
- // connection.setRequestProperty("Content-Length",xxx);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public Message readMessage() {
- Message vo = null;
- try {
- ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
- try {
- vo = (Message) ois.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return vo;
- }
- public void writeMessage(Message cv) {
- try {
- ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
- oos.writeObject(cv);
- oos.flush();
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public int getCode() {
- try {
- int code = connection.getResponseCode();
- System.out.println("code " + code);
- return code;
- } catch (IOException e) {
- e.printStackTrace();
- return 0;
- }
- }
- public static void main(String... args) throws IOException {
- BasicClient client = new BasicClient();
- client.connect();
- client.writeMessage(new Message());
- Message msg = client.readMessage();
- }
- }
- 通讯对象 必须实现可序列化,才可以传送对象
-
- import java.io.Serializable;
- public class Message implements implements Serializable {
- private String message;
- public getMessage(String message) {
- return this.message
- }
- public setMessage(String message) {
- this.message = message;
- }
- }