hello.jsp
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table>
<tr><th>Image</th></tr>
<%
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/lms", "lms", "lms");
Statement stmt=con.createStatement();
String strQuery = "select * from upload";
ResultSet rs = stmt.executeQuery(strQuery);
while(rs.next()){
rs.getString("img");
%>
<tr> <td>
<img src="img.jsp?imgimg=<%=rs.getString(1)%>" width="100" height="100">
</td>
</tr>
<%
}
rs.close();
con.close();
stmt.close();
}
catch(Exception e)
{
e.getMessage();
}
%>
</table>
</body>
</html>
img.jsp
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String id = request.getParameter("imgid");
try{
String img = request.getParameter("imgimg");
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/lms", "lms", "lms");
Statement st=con.createStatement();
String str ="select image from upload where img='"+img+"'";
ResultSet rs = st.executeQuery(str);
String imgLen="";
if(rs.next()){
imgLen = rs.getString(1);
}
rs = st.executeQuery(str);
if(rs.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
st.close();
response.reset();
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
%>
</body>
</html>