-
Notifications
You must be signed in to change notification settings - Fork 1
/
displayImage.jsp
67 lines (55 loc) · 1.57 KB
/
displayImage.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<%@ page trimDirectiveWhitespaces="true" import="java.sql.*,java.io.*" %><%@ include file="jdbc.jsp" %><%
// Indicate that we are sending a JPG picture
response.setContentType("image/jpeg");
// Get the image id
String id = request.getParameter("id");
if (id == null)
return;
int idVal = -1;
try{
idVal = Integer.parseInt(id);
}
catch(Exception e)
{ out.println("Invalid image id: "+id+" Error: "+e);
return;
}
// TODO: Modify SQL to retrieve productImage given productId
String sql = "SELECT productImage FROM product WHERE productId = ?";
String url = "jdbc:sqlserver://db:1433;DatabaseName=tempdb;";
String uid = "SA";
String pw = "YourStrong@Passw0rd";
try
{ // Load driver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch (java.lang.ClassNotFoundException e)
{
out.println("ClassNotFoundException: " +e);
}
try ( Connection con = DriverManager.getConnection(url, uid, pw);)
{
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1,idVal);
ResultSet rst = stmt.executeQuery();
int BUFFER_SIZE = 10000;
byte[] data = new byte[BUFFER_SIZE];
if (rst.next())
{
// Copy stream of bytes from database to output stream for JSP/Servlet
InputStream istream = rst.getBinaryStream(1);
OutputStream ostream = response.getOutputStream();
int count;
while ( (count = istream.read(data, 0, BUFFER_SIZE)) != -1)
ostream.write(data, 0, count);
ostream.flush();
istream.close();
}
}
catch (SQLException ex) {
out.println(ex);
}
finally
{
closeConnection();
}
%>