• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying an image in the jsp page from database.  RSS feed

 
Ravi Sankar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to display an image in the JSP page which is retrieved from the database. Do I need to have a getter/setter method in the Managed Bean? If yes, what will be the datatype for that property? If it is not needed, then how can i display that image in my jsp? I have given mycode which i implemented for your reference.
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

// Write image contents to response.
response.setContentType("image/jpeg");
response.setContentLength(length);
response.getOutputStream().write(bytes);

When using this code, I am getting the image alone in the next page. I have to include this image in that jsp page. Please help me how to solve this?


Regards
Ravi
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML 101:

Requests to a web server return a response of a single MIME type. In the event that that MIME type is "text/html", the returned page is expected to contain HTML elements. Some of those elements, such as IMG may contain URL refrerences themselves. In that event, additional requests are made by the browser to resolve those URLs. For the IMG element, that means that the URL should resolve to a MIME type of "image/xxxx", where "xxxx" is the encoding type, such as gif, jpeg or png.

The important thing here is that the image data is not embedded in the containing HTML, it's linked from the containing HTML. So the total number of requests made will be (at least) one for the page and one for each individual image on the page. Note that the image link doesn't have to refer to the same server or server app that the HTML came from. Or for that matter, that any other image, javascript or included CSS came from.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!