我的jsp,从DB恢复映像的servlet代码无法正常工作

时间:2016-09-10 09:18:58

标签: jsp servlets

我正在使用这些JSP,Servlet代码从MySql DB中检索图像但是无效。请帮忙。

DB:     id int;     icon blob;

<table class="table table-striped" style="position:absolute;width:201px; left:1138px ;top:425px ; border-top: 1px solid #DCDCDC;

background-color:#fff;“cellpadding =”0“cellspacing =”1“&gt;

<% while (rs2.next()) { %>
<TR style="font-family:Tahoma;font-size:13px;">

    <TD>
        <img src="photoServlet?id=<%=rs2.getString(1)%>" />
    </TD>
</TR>
<% } %>

 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 icon from tag where id=?");
        stmt.setLong(1, Long.valueOf(request.getParameter("id")));
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            response.getOutputStream().write(rs.getBytes("icon"));
        }
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }



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

1 个答案:

答案 0 :(得分:0)

在尝试了解这个问题的一些解决方案后,我使用了包含srvlet代码的jsp,现在图像显示正确。 tanx