3 Replies Latest reply on Apr 30, 2014 7:55 AM by jfclere

    How to append trailer headers with @WebServlet?


      We need to send large streams over the net via HTTP and we need to handle connection interrupts. We want to implement this via


      Transport-Encoding: chunked

      Trailer: Content-MD5


      as trailing header. The stream is send to the client and beside the sending, we use a stream to calculate the MD5 sum for the stream. The client shall be able to check the presence of the MD5 sum and the value of the MD5 sum to check for a complete and consistent stream.


      Can this be implemented with @WebServlet? The code is like that one:



      public class MyServlet extends HttpServlet {

          public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

              response.setHeader(HttpHeaderNames.TRAILER, HttpHeaderNames.CONTENT_MD5);



              ServletOutputStream outputStream = response.getOutputStream();

              /* Put some stuff into the stream with MD5 sum calculation... */


              response.setHeader(HttpHeaderNames.CONTENT_MD5, "the calculated MD5 sum");




      I have a response where we can put our content into the stream., but what do I need to do to add trailer headers? The code above does not work, because setHeader checks for a commit and ignores the request, but the used implementation of Tomcat included in JBoss 7.1.1 (the version we use and btw. what version of Tomcat is it?) shall be compatible with HTTP/1.1, but it seems not to be complete, does it? Is there a way to add trailer headers in another way? Is it possible with Resteasy via REST interface?

        • 1. Re: How to append trailer headers with @WebServlet?

          you need to add the headers _before_ committing the response.

          • 2. Re: How to append trailer headers with @WebServlet?

            The problem is, I want to add _trailing_ headers at the end of the HTTP response after the chunked body as the HTTP specification tells. I know, that I have to add a header to tell about the presence of a trailing header, what is not the issue. The issue is to put the actual trailing header at the end of the response. As far as I see it, there are only headers to be added to the response header, but not at the end. Or do I miss an option or functionality here?


            On the other side: One of the main functions of a trailing header is to add a checksum for instance, which can only be calculated at the end of the stream when the stream is already sent to the client. The functionality is killed, if I need to stream once for the checksum and a second time after adding the header to send everything to the client.

            • 3. Re: How to append trailer headers with @WebServlet?

              You have to write your own ChunkedOutputFilter to do that and you need your own application to process the trailers of each chunk that will be sent by the filter.