jsp中URL及其URLConnection的使用,重谢!!
我的li1.jsp如下:
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@ page import="java.io.*,java.net.*"%>
<html>
<%
//定义一个要传输的类
class A implements java.io.Serializable {
String name="nihao";
}
A obj=new A();
//定义URL
URL url=new URL("http://10.131.11.234:8080/li2.jsp");
//建立一个URLConnection
URLConnection connect = url.openConnection();
//进行设置使之可以写进读出,是否正确?
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(true);
connect.setAllowUserInteraction(false);
connect.connect();
ObjectOutputStream p=new ObjectOutputStream(connect.getOutputStream());
p.writeObject(obj);
p.close();
%>
</html>
相对应的li2.jsp 如下:
目的是接收发过来的对象写入li.txt
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,java.net.*"%>
<html>
<body>
<%
class A implements java.io.Serializable {
String name="nihao";
}
ObjectInputStream in=new ObjectInputStream(request.getInputStream());
File file=new File("li.txt");
ObjectOutputStream print=new ObjectOutputStream(new FileOutputStream(file));
try
{
A obj=(A)in.readObject();
print.writeObject(o);
}
catch(Exception e)
{}
in.close();
print.close();
%>
</body>
</html>
但是这样对象并没有传到li2.jsp,(地址是正确的),请诸位帮忙。