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 670513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:16:21+00:00 2026-05-14T00:16:21+00:00

I would like to convert BufferedImage to an image that will display on JSP

  • 0

I would like to convert BufferedImage to an image that will display on JSP page. How can I achieve this?

  • 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-14T00:16:21+00:00Added an answer on May 14, 2026 at 12:16 am

    First, JSP is a view technology providing a template to write HTML/CSS/JS in and the ability to interact with backend Java code to control page flow and access backend data. Your problem is more in HTML.

    Now, to display an image in a HTML page, you need the HTML <img> element. To define/allocate an image, you just have to let the src attribute point to an URL. E.g.

    <img src="url/to/image.jpg" />
    

    (it can be either relative to the current context, or an absolute URL, e.g. starting with http://)

    If the image is dynamic, as in your case, you need to have a Servlet which listens on the url-pattern matching the image URL. E.g.

    <img src="imageservlet/image.jpg" />
    

    (here the servlet is obviously to be mapped on an URL pattern of /imageservlet/* and the image identifier, here the filename, is here available by request.getPathInfo())

    The <img src> will fire a GET request, so you just have to implement doGet() method of the servlet. To send a HTTP response all you need to do is to write some content to the OutputStream of the response, along with a set of response headers representing the content (Content-Type, Content-Length and/or Content-disposition). You can use ImageIO#write() to write a BufferedImage to an OutputStream.

    You can find a basic example of such an image servlet here. You just have to replace Files#copy() with ImageIO#write().

    response.setContentType("image/png");
    ImageIO.write(bufferedImage, "png", response.getOutputStream());
    

    As a completely different alternative, you can also let the servlet convert the image to a Base64 encoded string and pass it on to the JSP:

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", output);
    String imageAsBase64 = Base64.getEncoder().encodeToString(output.toByteArray());
    request.setAttribute("imageAsBase64", imageAsBase64);
    request.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);
    

    And finally show it in the forwarded JSP using the data URI scheme as below:

    <img src="data:image/png;base64,${imageAsBase64}" />
    

    You only need to keep in mind that this doesn’t give the server nor the client the opportunity to cache the image. So this approach is plain inefficient in case the image is not temporary.


    See also:

    • How to retrieve and display images from a database in a JSP page?
    • Simplest way to serve static data from outside the application server in a Java web application
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to convert the below foreach statement to a LINQ query that
I have an ActiveRecord model that I would like to convert to xml, but
I have byte array as input. I would like to convert that array to
I have a Java Applet that is generating an image. Ultimately, I would like
I have an app running on the iPhone that I would like to convert
I would like to convert an absolute path into a relative path. This is
I would like to convert this string {id:1,name:Test1},{id:2,name:Test2} to array of 2 JSON objects.
I would like to convert this SQL statement to a JPQL equivalent. SELECT *
I would like to convert a PNG32 image (with transparency) to PNG8 with Python
I have a JSON string that I would like to convert to a javascript

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.