farajnew
farajnew

Reputation: 11

Downloading PDF file with additional data from a Servlet

How to download PDF file with other data on the same HTTP request to a Servlet? For example, I have a PDF file in my server and I want to respond to a request with that PDF file and other data like myname and myage, etc on the same request. Can it be done on the same request?

Upvotes: 1

Views: 1082

Answers (2)

AdrianRM
AdrianRM

Reputation: 2751

If this other data is just text, perhaps you could it include it as headers of the response

Upvotes: 0

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

The Java Servlet API does not provide any in-built mechanism for multi-part responses (which is the name of the feature that you are looking for). The Servlet API documentation hints at how this can be achieved, in the ServletResponse API doc:

To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.

Multipart responses are created by setting the content-type (the MIME type) of the response to "multipart/x-mixed-replace;boundary=xyz". The value xyz is arbitrary and is used to delineate the several sections of the response. An implementation of a Multipart Response class can be found in the book - "Java Servlet Programming" by Jason Hunter, and also in the KickJava site (please read the license before using it in your project).

Upvotes: 2

Related Questions