两个Web服务器交互

发送web应用服务器:

页面1:

<%@ page language="java" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body >
    <FORM action="servlet/SendServlet" method="post">
      <input type="submit" value="send"/></TD>
    </FORM>
  </body>
</html>

Servlet:

package com.order.web;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.bo.Employee;

public class SendServlet extends HttpServlet {

public SendServlet() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//远程WEB服务器的Servlet
String str_url = "http://127.0.0.1:8089/test/servlet/AcceptServlet";

List<Employee> list = new ArrayList();
try {
System.out.println("进入 Servlet !");

URL url = new URL(str_url);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.connect();

// 输入结果流
ObjectOutputStream oos = new ObjectOutputStream(urlConn
.getOutputStream());
Employee emp = new Employee();
emp.setName("长江");
list.add(emp);
oos.writeObject(list); // 写入 List
oos.flush();
oos.close();

// 返回结果流
ObjectInputStream ois = new ObjectInputStream(urlConn
.getInputStream());
List returnList = (List) ois.readObject(); // 里面存放的是List
ois.close();

System.out.println(returnList.get(0));

} catch (Exception ex) {
System.out.println(ex);

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

public void init() throws ServletException {
// Put your code here
}

}

java文件:

package common.bo;
/**
* 包路径需要和接收端一样
* serialVersionUID需要一样
*/

public class Employee implements java.io.Serializable {
private static final long serialVersionUID = "2008-08-28".hashCode();
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}
------------------------------------------------------------------

接收web应用服务器:

Servlet:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.bo.Employee;

public class AcceptServlet extends HttpServlet {

public AcceptServlet() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

List retuenList = new ArrayList();// 保存返回给web应用服务器list
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(response
.getOutputStream());
List acceppList = null;
try {
acceppList = (List) ois.readObject();// 接收发送web应用传来的list
System.out.println("接收到的人员姓名="
+ ((Employee) acceppList.get(0)).getName());
} catch (ClassNotFoundException e) {
retuenList.add("fail");
oos.writeObject(retuenList);
e.printStackTrace();
}

retuenList.add("success");
oos.writeObject(retuenList);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

public void init() throws ServletException {
// Put your code here
}

}


java文件:

package common.bo;
/**
* 包路径需要和发送端一样
* serialVersionUID需要一样
*/

public class Employee implements java.io.Serializable{
//test项目
private static final long serialVersionUID = "2008-08-28".hashCode();
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

你可能感兴趣的:(java,Web,应用服务器,.net,servlet)