Hey could anyone help me with my servlet which is meant to display images to my jsp page after grabbing them from the database. My image servlet below doesn't seem to be displaying the image in my browser at all, only a broken image icon:
@WebServlet(name = "ImageServlet", urlPatterns = {"/Image"})
public class ImageServlet extends HttpServlet {
private DatabaseConnector dataManager;
BufferedInputStream input = null;
BufferedOutputStream output = null;
@Override
public void init() {
dataManager = new DatabaseConnector();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Prepare.
String name = request.getPathInfo().substring(1); // Returns "foo.gif". You may want to do nullchecks here to avoid NPE's.
ResultSet resultSet = null;
try {
// Query DB.
resultSet = dataManager.getPhotos(1);
if (resultSet.next()) {
// Image found, prepare response and send it.
response.setContentType(resultSet.getString("contentType"));
response.setContentLength(resultSet.getInt("contentLength"));
response.setHeader("Content-Disposition", "inline;filename=\"" + name + "\"");
input = null;
output = null;
try {
input = new BufferedInputStream(resultSet.getBinaryStream("content"));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
for (int length; (length = input.read(buffer)) > -1;) {
output.write(buffer, 0, length);
}
} finally {
if (output != null) {
try {
output.close();
} catch (IOException logOrIgnore) {
}
}
if (input != null) {
try {
input.close();
} catch (IOException logOrIgnore) {
}
}
}
} else {
// No image found, send HTTP 404.
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException e) {
throw new ServletException("Something failed at SQL/DB level.", e);
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException logOrIgnore) {
}
}
}
}
}
The image in my jsp page looks like this:
<img src="/Image/<%= h.getHomeID() %>">
And finally my database connector class from which the result set comes from is:
public ResultSet getPhotos(String photoID) throws
IllegalArgumentException, SQLException, ClassNotFoundException {
createConnection();
ResultSet rs = null;
PreparedStatement preparedStatement = null;
String strQuery = "SELECT home_photo.photo "
+ "FROM home_photo "
+ "WHERE home_photo.home_id = ?";
try {
preparedStatement = conn.prepareStatement(strQuery);
preparedStatement.setString(1, photoID);
rs = preparedStatement.executeQuery();
} catch (SQLException e) {
throw new SQLException(e);
} finally {
closeConnection();
return rs;
}
}
My web.xml looks like this too:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>DB.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/Image/*</url-pattern>
</servlet-mapping>
If anyone would know why the image is not being displayed i woud be thankful