无法阻止输入流读取
我希望读取输入流以阻止而不是读取流末尾(-1)。有没有办法配置流来做到这一点?这是我的 Servlet 代码:
PrintWriter out = response.getWriter();
BufferedReader in = request.getReader();
try {
String line;
int loop = 0;
while (loop < 20) {
line = in.readLine();
lgr.log(Level.INFO, line);
out.println("<" + loop + "html>");
Thread.sleep(1000);
loop++;
//
}
} catch (InterruptedException ex) {
lgr.log(Level.SEVERE, null, ex);
} finally {
out.close();
}
这是我的 Midlet 代码:
private HttpConnection conn;
InputStream is;
OutputStream os;
private boolean exit = false;
public void run() {
String url = "http://localhost:8080/WebApplication2/NewServlet";
try {
conn =
(HttpConnection) Connector.open(url);
is = conn.openInputStream();
os = conn.openOutputStream();
StringBuffer sb = new StringBuffer();
int c;
while (!exit) {
os.write("<html>\n".getBytes());
while ((c = is.read()) != -1) {
sb.append((char) c);
}
System.out.println(sb.toString());
sb.delete(0, sb.length() - 1);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
os.close();
is.close();
conn.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
我尝试过 InputStream.read,但它也不会阻塞,它也返回 -1。 我试图让两侧的 I/O 流保持活动状态。 我希望 Servlet 等待输入、处理输入,然后发回响应。
在上面的代码中应该执行 20 次。
感谢您的帮助
I would like the input stream read to block instead of reading end of stream (-1). Is there a way to configure the stream to do this? Here's my Servlet code:
PrintWriter out = response.getWriter();
BufferedReader in = request.getReader();
try {
String line;
int loop = 0;
while (loop < 20) {
line = in.readLine();
lgr.log(Level.INFO, line);
out.println("<" + loop + "html>");
Thread.sleep(1000);
loop++;
//
}
} catch (InterruptedException ex) {
lgr.log(Level.SEVERE, null, ex);
} finally {
out.close();
}
Here's my Midlet code:
private HttpConnection conn;
InputStream is;
OutputStream os;
private boolean exit = false;
public void run() {
String url = "http://localhost:8080/WebApplication2/NewServlet";
try {
conn =
(HttpConnection) Connector.open(url);
is = conn.openInputStream();
os = conn.openOutputStream();
StringBuffer sb = new StringBuffer();
int c;
while (!exit) {
os.write("<html>\n".getBytes());
while ((c = is.read()) != -1) {
sb.append((char) c);
}
System.out.println(sb.toString());
sb.delete(0, sb.length() - 1);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
os.close();
is.close();
conn.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I've tried InputStream.read, but it doesn't block either, it returns -1 as well.
I'm trying to keep the I/O streams on either side alive.
I want the servlet to wait for input, process the input, then send back a response.
In the code above it should do this 20 times.
thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据JavaDoc:
http ://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read()
因此,只要您在一端输入缓冲区,它就会在另一端消耗它。
According to the JavaDoc:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read()
So as long as you feed the buffer on one end, it will consume it on the other.
Servlet 的工作方式是一种简单的请求回复机制。在 Servlet 中,您从 Reader 读取数据,然后在 Writer 中发送回复。
在 3.0 之前的普通 Servlet 或 Comet 或 Continuations 中,您无法选择执行任何其他操作。您唯一可以做的就是创建一个会话并让客户端发回另一个请求以获取更多信息(您可以使用请求中的某些内容来执行与会话相同的操作)。
您似乎正在寻找一种保持连接打开的方法。为此,您有多种解决方案,这些解决方案可能有效也可能无效,具体取决于您的防火墙配置。
1) 升级到支持Servlet 3.x规范的Servlet引擎
2) 使用Tomcat Comet。
3) 使用 Jetty Continuations。
4) 使用网络套接字。
5) 使用 KEEP_ALIVE 并让客户端轮询打开的连接
注意:您永远不应该在服务器代码中放置睡眠,这是一个立即标志,表明您做错了什么。
The way a Servlet works is a simple request reply mechanism. In the Servlet you read the data from the Reader and then send a reply back in the Writer.
In plain old Servlets prior to 3.0 or Comet or Continuations you do not have the option of doing anything else. The only thing you can do is create a Session and have the client send back another request to get more info (You could use something in the Request to do the same thing as a Session).
What it seems you are looking for is for a way to keep your Connection open. For that you have several solutions that may or may not work depending on your firewall configuration.
1) Upgrade to a Servlet engine that supports the Servlet 3.x specification
2) Use Tomcat Comet.
3) Use Jetty Continuations.
4) Use Web Sockets.
5) Use a KEEP_ALIVE and have the client poll the open connection
As a note: You should never put a Sleep in your server code, that is an immediate flag that you are doing something wrong.
您正在寻找异步 IO。这是内置于 Servlet 3.0(包括示例应用程序)中的。如果您仍在使用 Servlet 2.5 或更早版本,请查看 Comet 技术,这里有一个 Tomcat 目标 文档。
You're looking for asynchronous IO. This is builtin in Servlet 3.0 (sample app included). If you're still on Servlet 2.5 or older yet, then have a look at Comet technique, here's a Tomcat-targeted document.