从数据库检索图像并使用JSTL在JSP中显示它 [英] Retrieve images from database and display it in JSP using JSTL

查看:79
本文介绍了从数据库检索图像并使用JSTL在JSP中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设用户访问我的网站时,他将看到很多以前存储在数据库中的图像.我知道如何在JSP Scriptlet中做到这一点,也知道当用户使用Servlet提交表单时如何从JSTL中的数据库中获取和检索数据.但是我不知道如何在不提交用户表单的情况下在JSTL中做到这一点.

Assume that when a user accesses to my website, then he will see a lot of images which have stored in the database before. I know how to do that in JSP Scriptlet, and I also know how to fetch and retrieve data from the database in JSTL when the user submit a form by using servlet. But I don't know how to do it in JSTL without user submitting a form.

推荐答案

是的,您可以使用 JSTL & EL .对于数据库访问,请使用JSTL SQL标记库.

Yes, You can use JSTL & EL. For database access use JSTL SQL Tag library.

如何在存储在数据库中的JSP中显示图像?

How to display images in JSP that is stored in database?

我希望您使用BLOB类型列将图像存储在数据库中.只需点击传递记录ID的Servlet,然后发送byte[]作为响应.

I hope you are using BLOB type column to store images in database. Simply hit a Servlet passing id of the records and send byte[] in response.

我为每个图像创建了单独的请求,以提供更好的用户体验.

I have created separate request for each image for better user experience as well.

注意:最好在Servlet中移动数据库代码.

Note: It's better to move the database code in the Servlet.

JSP:

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<sql:setDataSource var="webappDataSource"
    driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"
    user="root" password="root" />

<sql:query dataSource="${webappDataSource}"
    sql="select id,username from users" var="result" />

<table width="100%" border="1">
    <c:forEach var="row" items="${result.rows}">
        <tr>
            <td>${row.id}</td>
            <td>${row.username}</td>
            <td>
               <img src="${pageContext.servletContext.contextPath }/photoServlet?id=${row.id}" />
            </td>
        </tr>
    </c:forEach>
</table>

Servlet(PhotoServlet.java):

Servlet (PhotoServlet.java):

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:3306/test";
    final String User = "root";
    final String Password = "root";
    try {
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, User, Password);

        PreparedStatement stmt = conn.prepareStatement("select photo from users where id=?");
        stmt.setLong(1, Long.valueOf(request.getParameter("id")));
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            response.getOutputStream().write(rs.getBytes("photo"));
        }
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

web.xml:

<servlet>
    <servlet-name>PhotoServlet</servlet-name>
    <servlet-class>com.server.servlet.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PhotoServlet</servlet-name>
    <url-pattern>/photoServlet</url-pattern>
</servlet-mapping>

表结构:(用户)

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
| password | varchar(20) | YES  |     | NULL    |       |
| photo    | blob        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

这篇关于从数据库检索图像并使用JSTL在JSP中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆