-
问题内容:怎样利用URLConnection获得servlet里的输出变量值
- 原讨论链接:http://community.csdn.net/expert/topicview1.asp?id=5191818
- 所属论坛:Web 开发
审核组:JAVA
- 提问者:silverswords
解决者:masse
- 感谢:masse hbwhwang ruanjiantaotao
- 关键字:Java Servlet Web 开发 http url new 输出 localhost datainputstream 节流 scxt servletapplet
- 答案:
我在servlet里这样输出
nowtime="2006-11-28 17:02:26";
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteOut);
out.writeUTF(nowtime);
//out.write(100);
out.close();
-----------------------
然后在另外一个类里
URLConnection urlcon=null;
URL url = new URL("http://localhost:8080/scxt/servletapplet");
urlcon = url.openConnection();
DataInputStream in = new DataInputStream(urlcon.getInputStream());
System.out.print("get data:");
System.out.println(in.read());
//System.out.println(in.readUTF());
in.close();
-----------------------------------
但我执行后,发现根本没有得到,用readUTF则报io异常,用read,则返回-1
真给java的输入输出搞晕了
-_-!
---------------------------------------------------------------
首先在浏览器访问:http://localhost:8080/scxt/servletapplet
看看有没有输出。
如果没有输出,说明是servlet有问题。
获取servlet输出的代码:
URL url = new URL("http://localhost:8080/scxt/servletapplet");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
---------------------------------------------------------------
RESPONSE发回来的是文本,怎么可能用DataInputStream呢?
---------------------------------------------------------------
RESPONSE返回的应该是网页中的字节流,可以转化成DataInputStream
- 评价:
给朵鲜花(2)
扔个鸡蛋(0)