关于Servlet的http隧道的问题,请高手指点

wylove 2003-03-10 02:18:49
*---可序列化的类---------------------------------------------------------------
import java.io.*;
import java.util.*;

public class StudentList implements Serializable
{
//声明一个Vector用于存储学生信息
Vector list = new Vector(6);
//Constructor
public StudentList(){
}
//加入同学信息
public void addStudent(String value){
if (value!=null)
{
list.addElement(value);
}
}
//显示同学信息
public void listStudents(){
for (int x=0;x<list.size();x++)
{
System.out.println("Student "+x+" :"+(String)list.elementAt(x));
}
}
}

*-------调用Servlet的连接,从而进行隧道通信-----------------------------------------------------------
import java.io.*;
import java.net.*;

public class StudentHttpApplication
{
//Default Constructor
public StudentHttpApplication()
{
}
//建立同学信息表
public void buildStudentList(StudentList value){
value.addStudent("代英");
value.addStudent("刘凤");
value.addStudent("袁炳");
}
//序列化写
public void putStudentList(URLConnection connection,StudentList value){
try
{
connection.setUseCaches(false);
connection.setRequestProperty("CONTENT-TYPE","application/octet-stream");
connection.setDoInput(true);
connection.setDoOutput(true);
ObjectOutputStream os = new ObjectOutputStream(connection.getOutputStream());
os.writeObject(value);
os.flush();
os.close();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
//序列化读
public StudentList getStudentList(URLConnection connection){
StudentList list = null;
try
{
ObjectInputStream is = new ObjectInputStream(connection.getInputStream());
list=(StudentList)is.readObject();
is.close();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
} catch(ClassNotFoundException ex){
System.out.println(ex.getMessage());
}
return list;
}
//执行程序
public void invoke(){
try
{
StudentList list = new StudentList();
buildStudentList(list);
URL url = new URL("http://localhost/myServlet/StudentTunniingWorld");
URLConnection con = url.openConnection();
putStudentList(con,list);
StudentList inList = getStudentList(con);
if (inList!=null)
{
inList.listStudents();
} else {
System.out.println("Read object failed!");
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
public static void main(String[] args)
{
StudentHttpApplication studentApp = new StudentHttpApplication();
studentApp.invoke();
}
}
*---------响应客户端请求的Servlet---------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class StudentTunniingWorld extends HttpServlet
{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}

public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
try
{
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
StudentList list =(StudentList)ois.readObject();
list.listStudents();
response.setContentType("application/octest-stream");
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(list);
oos.flush();
oos.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
public String getServletInfo(){
return "StudentTunningWorld Information";
}
}

请高手指教,为什么我在客户端收不到通过隧道传递的信息?
...全文
47 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxisxx 2003-03-11
  • 打赏
  • 举报
回复
gz

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧