This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes Display of image in JSP from data base using servlet    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic

Display of image in JSP from data base using servlet

Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Hi All,

I am getting the image from the database (in a servlet), I want to display that image in JSP. But in JSP i am getting a broken image instead of an proper image.

Here is the servlet code :

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strImageName = req.getParameter("name");
res.setContentType("image/jpeg"); //JPG file
conn = DisableText.getConnection(); // Connecting to the database
pstmt = conn.prepareStatement("select image from image_upload where image_name=?");
rs = pstmt.executeQuery();
byte[] imgBytes = rs.getBytes(1);
catch (Exception e)
throw new ServletException("Unable to print image"+e.getMessage());

Here is the JSP code:
<form name="delete" action="download" method="post" >
<IMG src="<%=request.getContextPath() %>/DisableText"> // DisableText is the servlet class name

Can any one help me please.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Display of image in JSP from data base using servlet
It's not a secret anymore!