的index.jsp 这是我的jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<sql:setDataSource var="webappDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/bharatwellness"user="root" password="naveen" />
<sql:query dataSource="${webappDataSource}" sql="select * from individaulpartner " var="result" />
<table width="100%" border="1">
<c:forEach var="row" items="${result.rows}">
<tr>
<td>${row.id}</td>
<td>${row.fname}</td>
<td>
<img src="${pageContext.servletContext.contextPath }/ImageServlet?id=${row.id}" />
</td>
<td>
<a href="${pageContext.servletContext.contextPath }/ImageServlet?id=${row.id}" />certificate</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
ImageGetTest.java 这是我的servlet代码
package com.server.servlet;
import java.io.IOException;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/mydatabase";
final String User = "root";
final String Password = "password";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, User, Password);
PreparedStatement stmt = conn.prepareStatement("select * from usertable where id=?");
stmt.setLong(1, Long.valueOf(request.getParameter("id")));
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
response.getOutputStream().write(rs.getBytes("image"));
response.getOutputStream().write(rs.getBytes("certificate1"));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我从中获取数据的表格
我只能为一个ID显示单个图片或单个证书。如何在jsp页面上显示所有文件。请帮帮我......
答案 0 :(得分:1)
您需要为每个单独的图像调用Sevlet。所以在你的JSP中你需要做类似的事情:
<td>
<img src="${pageContext.servletContext.contextPath }/ImageServlet?id=${row.id}&name=image" />
<img src="${pageContext.servletContext.contextPath }/ImageServlet?id=${row.id}&name=certificate" />
</td>
并在您的Servlet中检查附加的“name”参数以确定要发回的图像:
if (rs.next()) {
if(request.getParameter("name").equals("image")
response.getOutputStream().write(rs.getBytes("image"));
}else{
response.getOutputStream().write(rs.getBytes("certificate1"));
}
}
所有这一切的问题在于它不是动态的。为了使它能够处理给定实体的任意数量的图像,您需要在处理JSP时了解可用的图像,并且有一个循环,它将为每个图像生成<img/>
标记,并使用必要的参数。
这也可能涉及一些数据库更新:图像与相应的用户进入带有FK的不同表格。
img_id user_id image_data
1 1 bytes
2 1 bytes
3 1 bytes
4 1 bytes
现在,您将获得相关用户的图像ID句柄,并通过将id传递给Servlet来逐个加载它们。