[英]I want to display multiple images from database into jsp (i am mapping servlet) so in jsp m gonna display in src of img tag
I am using longblob data type in a table to store image,At least, five images are stored, I want to retrieve all images from the database and want to display on jsp which consists of image tag, an image tag's attribute src is assigned to the servlet name as src="./Serv1", this Serv1 contains image retrieved from database but the problem is I have no idea how to show multiple images, It is showing only the first image, should I use loop if yes then how? 我在表中使用longblob数据类型存储图像,至少存储了五张图像,我想从数据库中检索所有图像,并想在包含图像标签的jsp上显示,图像标签的属性src被分配给servlet名称为src =“ ./ Serv1”,此Serv1包含从数据库中检索到的图像,但问题是我不知道如何显示多个图像,它仅显示第一个图像,如果可以,我应该使用循环吗?
I have this in JSP page 我在JSP页面中有这个
while(r.next())
{
%>
<img src="./Serv1" height="100" width="200">
<p>Product <%=r.getInt(1)%>: <%=r.getString(2)%></p>
and my servlet whose url-pattern is Serv1 has this code 而我的url模式为Serv1的servlet具有此代码
ResultSet r=st.executeQuery("select prodimg from product;");
if(r.next()){
img= r.getBlob(1);
imgbyte=img.getBytes(1, (int)img.length());
response.setContentType("image/jpg");
oos=response.getOutputStream();
}
oos.write(imgbyte);
con.close();
Suppose you have jsp page where you want to retrieve image. 假设您有要检索图像的jsp页面。 You can do something like this to retrieve any image from database.
您可以执行类似的操作以从数据库中检索任何图像。
<% // dbconnection
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
Statement statement = conn.createStatement() ;
resultSet=statement.executeQuery("select * from product") ;
%>
<!--this loop will get all images-->
<% while(resultSet.next()){ %>
<!--I'm using id column of table,you can use any coulmn which is unique to all row-->
Image - <img src="./Serv1?id=<%=resultSet.getString("id")%>" width="20%"/>
< p>Product <%=r.getInt(1)%>: <%=r.getString(2)%></p>
<%
}
}catch(Exception e){}
%>
In Above code this -> <img src="./Serv1?id=<%=resultSet.getString("id")%>" />
line is important ,here you are passing parameter
ie : id
to servlet to get particular image
在上面的代码中,此->
<img src="./Serv1?id=<%=resultSet.getString("id")%>" />
行很重要,这里您将parameter
即: id
传递给servlet以获取特定信息image
Now,in your servlet
ie ./Serv1
you have to retrieve the id
in doGet
and pass in query ,lastly send back the reponse to jsp page . 现在,在您的
servlet
即./Serv1
您必须在doGet
检索id
并传递查询,最后将响应发送回jsp页面。
Blob image = null;
byte[] imgData = null;
String id= request.getParameter("id");//here you are getting id
int i;
ResultSet rs =null;
try {
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
String sql = "SELECT prodimg FROM product where id=?"; //here pass that id in query to get particular image
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
while (rs.next()) {
image = rs.getBlob("image");//getting image from database
imgData = image.getBytes(1,(int)image.length()); //extra info about image
}
response.setContentType("image/gif");//setting response type
OutputStream o = response.getOutputStream();
o.write(imgData);//sending the image to jsp page
o.flush();
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Also this is not complete code,make changes as per your requirements .also don't forget to add jar's file
这也不是完整的代码,请根据您的要求进行更改。也不要忘记添加
jar's file
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.