request.getReader()方法为什么调用一次以后就再也不能取出数据呢?

Philip1314 2003-08-16 03:25:33
在同一个JSP页面中
java.io.BufferedReader rd = request.getReader();
String str;
while ( (str=rd.readLine())!=null) {
System.out.println(str);
}

//第二次
rd = request.getReader();
while ( (str=rd.readLine())!=null) {
System.out.println(str);
}

为什么第二次就打印不出任何数据呢?
...全文
1877 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
one_bird 2003-08-16
  • 打赏
  • 举报
回复
学习~!
whodsow 2003-08-16
  • 打赏
  • 举报
回复
ServletRequest的getReader()方法及其getInputStream()方法均只能被调用一次,而且前者与后者也是冲突的,调用了前者就不能再调用后者,如Servlet的document:
--------------------------------------------------------------------------------

getInputStream
public ServletInputStream getInputStream()
throws java.io.IOExceptionRetrieves the body of the request as binary data using a ServletInputStream. Either this method or getReader() may be called to read the body, not both.
Returns:
a ServletInputStream object containing the body of the request
Throws:
java.lang.IllegalStateException - if the getReader() method has already been called for this request
java.io.IOException - if an input or output exception occurred



--------------------------------------------------------------------------------

getReader
public java.io.BufferedReader getReader()
throws java.io.IOExceptionRetrieves the body of the request as character data using a BufferedReader. The reader translates the character data according to the character encoding used on the body. Either this method or getInputStream() may be called to read the body, not both.
Returns:
a BufferedReader containing the body of the request
Throws:
java.io.UnsupportedEncodingException - if the character set encoding used is not supported and the text cannot be decoded
java.lang.IllegalStateException - if getInputStream() method has been called on this request
java.io.IOException - if an input or output exception occurred
See Also:
getInputStream()
Shield Pai 2003-08-16
  • 打赏
  • 举报
回复
第二次打印出什么? 有异常信息吗?如果没有任何信息,那说明这个方法只能调用一次.参考一下API吧.

81,094

社区成员

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

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