MDP
MDP

Reputation: 4267

How to pass an InputStream from a jsp to a servlet

I have to print on a jsp page some images that i get in the form of InputStream.

First i have servlet that passes a variable containing an InputStream to a jsp page, this way:

request.setAttribute("Image", InputStream);
request.getRequestDispatcher(pagename).include(request, response);

In my jsp page i have this to get that InputStream:

${requestScope.VariableContainingInputStream}

To turn that InputStream into an image i should use a servlet this way:

<img src="ServletName">

How can i pass that InputStream to that servlet?

Upvotes: 0

Views: 2215

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149035

If you get it as an InputStream, I assume the image is generated dynamically or generally speaking that you have something that gives it to you depending of a number of parameters.

You should think about how a normal (or stupid ...) browser will work :

  • the user clicks on a link, a submit button or pass an url in adress bar
  • the browser generate the corresponding request and sends it to the server
  • the server generate a (generally HTML) page containing links to images and sends it back to browser
  • the browser analyzes the page, and sends separate requests for the images
  • the server sends back the image one for each request
  • the browser display full page containing images

(you could replace images by css pages, js scrips, or any other resources)

So you should not get the input stream at the time of running your jsp to compose the HTML page but write in it <image source=/ImageServlet_url?params_for_current_image/>

Then when the browser will ask for the image, the image servlet will ask for the InputStream and put it directly in response body, with the correct type in the response headers.

This is by far the most robust way of solving your problem. If really it is not an option and the InputStream is only disponible at the moment of running the jsp, you must put in in a session attribute. Then when the ImageServlet will be called, it will look for it in the session and sends it. The problem is that an InputStream in not necessarily Serializable and it is unsafe to put non serializable items in session. So you should :

  • set a global Hash<String, InputStream> somewhere in your app
  • when trying to put the InputStream in session, actually put in in the hash (with a unique key) and store the key in session
  • when getting the InputStream from session, get the key from the session and fetch the InputStream from the hash ... and do not forget to remove it form the hash ...

I strongly advice you to stick to the first solution, not speaking or network errors or power outages between the request of the HTML page and the image ...

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280064

How can i pass that InputStream to that servlet?

You wouldn't. Your JSP would create a temporary (or permanent) file and would write the contents of the InputStream to it. You'd then provide an endpoint that would serve up the content of that file.

You would then provide the URL to that endpoint in your JSP's <img> element.

Upvotes: 1

Related Questions