客户端使用 URL 对象将数据传递给 servlet
我的客户端首先使用服务器的 url(包含 servlet)创建一个 url 对象,并使用以下代码将数据发送到 servlet:
URL url = new URL("http://localhost:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
out.writeObject(stringToReverse);
从服务器接收到所需的数据后,客户端再次需要向 servlet 发送数据。我是否应该关闭上述 ObjectOutptStream 并在同一连接中创建一个新的 ObjectOutptStream 来发送数据?应该怎么做呢?
我的另一个问题是,每次将数据写入客户端的输出流时,是否应该在 servlet 中创建一个单独的 ObjectInputStream?
My client first creates a url object with the url of the server (containing the servlet) and sends data to the servlet using the following code:
URL url = new URL("http://localhost:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
out.writeObject(stringToReverse);
After receiving the required data from the server, the client again needs to send a data to the servlet. Should I close the above ObjectOutptStream and create a new one within the same connection to send the data? How should it be done?
Another question that I have is that each time I write data into the outputstream of the client, should I create a separate ObjectInputStream in the servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于这些都是特定于数据发送后立即结束的连接的,因此我建议每次建立连接时都实例化它们。这些对象是使用构造函数而不是 setter 方法来初始化的,这表明这些类不打算一次又一次地重用。
Since these are all specific to a connection that ends as soon as the data is sent, I would suggest just instantiating them each time you make a connection. These objects are initialized using constructors instead of setter methods, and this suggests that the classes were not intended to be reused over and over again..