简体   繁体   English

是否可以从servlet向客户端返回多个值?

[英]Is it possible to return more than one value from a servlet to client?

I have a servlet which generates a .png image and writes it into the OutputStream as a byte array. 我有一个servlet,它生成一个.png图像并将其作为字节数组写入到OutputStream中。

I'd like to create an application which fires that servlet and downloads the generated image. 我想创建一个应用程序,该应用程序会触发该servlet并下载生成的图像。 I also would like to measure how much time does it take for the servlet to generate the image not including the time of parameter checking and such things. 我还想测量servlet生成图像所花费的时间,这不包括参数检查的时间和诸如此类的时间。 I know it's impossible from a client side application so the servlet itself should measure this time then send the measured value back to the client side app. 我知道在客户端应用程序中这是不可能的,因此servlet本身应该测量该时间,然后将测量值发送回客户端应用程序。

My problem : how is it possible to send more than one thing from a servlet: the image and the time value as well? 我的问题是 :如何从Servlet发送多个内容:图像和时间值?

Shall I use for example JSON? 我应该使用例如JSON吗? Or if I write the image (as a byte array) and the time value (as a long value) into the OutputStream how can I read it out on client side? 或者,如果我将图像(作为字节数组)和时间值(作为长值)写入OutputStream如何在客户端读取它?

In that case, you can send a header parameter. 在这种情况下,您可以发送标头参数。

long startTime = System.currentTimeMillis();

// Generate the image

long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
response.setHeader("ElapsedTime", elapsedTime);

It depends on your client. 这取决于您的客户。 As you mentioned servlet deals with bytes. 正如您提到的,servlet处理字节。 It can write to output stream what you want using protocol that you choose. 它可以使用您选择的协议将所需的内容写入输出流。 For example write int value that contains processing time in milliseconds followed by the byte array that contains your image. 例如,write int值包含以毫秒为单位的处理时间,后跟包含图像的字节数组。

But what kind of client will read this stream? 但是什么样的客户会读这个流? If it is programatic thick client you can implement similar logic there. 如果它是程序化的胖客户端,则可以在那里实现类似的逻辑。 But I believe it is expected to be a web browser that creates HTTP request because it is rendering HTML with <img> tag. 但是我相信它应该是创建HTTP请求的Web浏览器,因为它使用<img>标签呈现HTML。 But browser does not know to get additional data. 但是浏览器不知道要获取其他数据。

If however your you are using AJAX to get your image you can put additional data into HTTP response header and then read its value at client side. 但是,如果您使用AJAX获取图像,则可以将其他数据放入HTTP响应标头中,然后在客户端读取其值。

Yes we can return more than one value to client from HttpServletResponse. 是的,我们可以从HttpServletResponse向客户端返回多个值。

From the servlet version 2.2 Servlets have also been given the ability to send multiple values for the same response header using methods in HttpServletResponse. 从Servlet 2.2版开始,还赋予Servlet使用HttpServletResponse中的方法为同一响应头发送多个值的能力。

The new addHeader(String name, String value) method sets the header to the given value. 新的addHeader(String name,String value)方法将标头设置为给定值。 While the traditional setHeader() method would replace any existing value or values, addHeader() leaves current settings alone and just sets an additional value. 传统的setHeader()方法将替换任何一个或多个现有值,而addHeader()则将当前设置保留下来,仅设置一个附加值。 There's also addIntHeader(String name, int value) and addDateHeader(String name, long date). 还有addIntHeader(字符串名称,int值)和addDateHeader(字符串名称,长日期)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM