Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:14:46+00:00 2026-05-27T09:14:46+00:00

I have a servlet which is used to display image.This servlet actually called by

  • 0

I have a servlet which is used to display image.This servlet actually called by the

<img src="/displaySessionImage?widgetName=something"/>

My get & post redirect to this method,

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession();
    String widgetName = request.getParameter("widgetName");     

    try {
                    //this is my file manager which was store ealier
        StorageFile file = (StorageFile)session.getAttribute(widgetName);      
        response.setContentType(file.getContentType());         

                    //the file manager can retrieve input stream
        InputStream in  = file.getInputStream();
        OutputStream outImage = response.getOutputStream();

        byte[]  buf   = new byte[1024];
        int     count = 0;
        while ((count = in.read(buf)) >= 0) {
            outImage.write(buf, 0, count);
        }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

But this code does not work, the image could not be display. I think this will not work because i have store the file manager that contain the input stream in a session. This same method work for another image file that was retrieved from database and not stored in the session. i have actually print out the input stream. it contain the same input stream as the database file.

Is it something wrong with the code?
or i actually cannot store the file manager that contain the input stream in a session?
or is it that i used input stream in a wrong way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:14:47+00:00Added an answer on May 27, 2026 at 9:14 am

    You are really not clear about what is actually happening, which is perhaps just ignorance. But storing and passing an InputStream around in the session is already not a good sign. Firstly, it is not serializable. Secondly, you’re fully detaching the input stream from the context where it’s been created (so it might implicitly have been closed/released when the initial context is finished). Thirdly, an input stream can often be read only once (so once it’s read, it cannot be read again anymore, you’d have to create a new one).

    The normal approach is to read the InputStream into a byte[] directly after its creation and then store that byte[] in the session instead.

    InputStream input = uploadedFile.getInputStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    // Copy bytes from input to output the usual way.
    // ...
    
    byte[] content = output.toByteArray();
    // Now store it in session.
    

    And then in the image servlet, just do

    // ...
    response.getOutputStream().write(content);
    

    You only need to be aware that each byte of a byte[] eats one byte of JVM’s memory. Be sure that you don’t go overboard. Remove the attribute from the session as soon as you don’t need it anymore. Make use of temp file storage if necessary, for sure if you have to deal with large files.


    Update: as per your comment on the question:

    I am using firebug, response tab is empty, in header tab, response header contain : content-type : image/jpeg, content-length :0 , the server and the date.

    A content length of 0 confirms that the input stream was already been read (or its source has implicitly been released). This only confirms my initial guesses. No, manually setting the content length header won’t solve the problem. The servlet container already automatically takes care about it when the response body fits fully in the default response buffer; it would otherwise switch to chunked encoding anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a servlet which takes us to an existing jsp, say home.jsp. This
I have a servlet which handles http get requests that I'd like to be
I have a JSP page which display a list from servlet, it has a
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have a servlet S which handles callbacks from a 3rd party site. The
I'm programming using GWT, which includes Jetty. I have defined my own servlet and
I have a servlet that is used for many different actions, used in the
I have an application which provides services using CXF's Servlet transport and Jetty 6.1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.