2014-01-25 30 views
0

我试图从我的jsp页面的某个部分展示从mysql获取的图像。问题是无论在哪里放置代码,我只会获取图像,而不是页面上的其他内容。我想要从查询结果返回的图像顶部的导航栏和其他所有内容。 该代码:在jsp页面上显示一个mysql图像

<%@page import="java.sql.*" %> 
<%@page import="java.io.*" %> 
<%!public static Connection connect(){ 
    Connection con = null; 
    try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 


    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imperia","root", ""); 
    return con; 
    } catch (Exception e) { 
     throw new Error(e); 
    } 
} 
public static boolean close (Connection c) 
{ 
    try{ 
     c.close(); 
     return true; 
    } 
    catch (Exception e) 
    { 
     return false; 
    } 
} 
%> 
<%@ page language="java" contentType="text/html; charset=windows-1255" 
    pageEncoding="windows-1255"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <link href="css/bootstrap.min.css" rel="stylesheet"> 
     <link href="css/styles.css" rel="stylesheet"> 

     <div id="Imperia_Bank"> 
      <p>Imperia Bank</p> 
     </div> 
     <div id = "User" > 
     <p> Hello: Administrator </p> 
     </div> <div id = "User" > 

     </div> 
    <script type="text/javascript"> 

      $(function(){ 
       $('.fadein img:gt(0)').hide(); 
       setInterval(function(){ 
        $('.fadein :first-child').fadeOut() 
         .next('img').fadeIn() 
         .end().appendTo('.fadein');}, 
       3000); 
      }); 

     </script> 

    </head> 
    <body> 


     <div class="navbar navbar-inverse navbar-static-top"> 
     <div class="container"> 

      <a href="#" class="navbar-brand">Imperia Bank</a> 

      <button class="navbar-toggle" data-toggle="collapse" 
       data-target=".navHeaderCollapse"> 
       <span class="icon-bar"></span> <span class="icon-bar"></span> <span 
        class="icon-bar"></span> 
      </button> 

      <div class="collapse navbar-collapse navHeaderCollapse"> 

       <ul class="nav navbar-nav navbar-right"> 

        <li class="active"><a href="homepage.html">Home</a></li> 
        <li><a href="Login.html">Login</a></li> 
        <li><a href="Register.html">Register</a></li> 
        <li><a href="customer.html">Clients</a></li> 
        <li><a href="Administrator.html">Administrator</a></li> 
        <li><a href="account.html">Accounts</a></li> 
        <li><a href="Illustration.html">Illustration</a></li> 
        <li ><a href="ATM.html">Atm</a></li> 

       </ul> 

      </div> 

     </div> 
    </div> 
    </div> 
     <ul id="navigation"> 

     <div id="red_line_head"></div> 
     <div id="main"> 
      <td id="description"> 
       <p><p><br><br>Founded in 1992 as a Finance and Securities Company, 
       Imperia Bank converted into a fully fledged commercial bank in January 1996. 
       Since then the bank has had a long standing tradition of achieving strong financial performance and carrying out 
       expansion strategies while successfully focusing on efficient client service delivery. 
       Over the last 20 years, Imperia Bank has achieved a sustained growth in our customer deposit base which is largely attributed to the level of confidence our customers have in the bank and our corporate strategy. 
       Currently we have 23 branches in our expanding branch network across major towns and cities.<br><br> 
        Imperial Bank will continue to enhance the existing risk management parameters through the effective use of our newly installed and cutting edge core banking system. 
        As part of our growth strategy, we are also continuing to recruit qualified professional staff, and providing appropriate training to our existing human resources to ensure we continue to meet evolving customer demands. 
       </p> 
      </td> 
      <td id="news"> 
       <p> 
       <br><br><br><u>NEWS:</u> 
       <br><br> 
         <td> <div class="fadein"> 
    <img src="http://farm3.static.flickr.com/2610/4148988872_990b6da667.jpg"> 
    <img src="http://farm3.static.flickr.com/2597/4121218611_040cd7b3f2.jpg"> 
    <img src="http://farm3.static.flickr.com/2531/4121218751_ac8bf49d5d.jpg"> 
</div> 


</td> 
<table border="0" > 
    <tr>  
    </tr> 
    <tr> 
    <td><% 
    Blob image = null; 

    Connection con = null; 

    byte[] imgData = null; 

    Statement stmt = null; 

    ResultSet rs = null; 

    try { 

    con=connect(); 
     stmt = con.createStatement(); 

     rs = stmt 
       .executeQuery("select Picture from Imperia.homepage where idpicture= '1'"); 

     if (rs.next()) { 

      image = rs.getBlob(1); 

      imgData = image.getBytes(1, (int) image.length()); 

     } else { 

      out.println("Display Blob Example"); 

      out.println("image not found for given id>"); 

      return; 

     } 

     // display the image 

     response.setContentType("image/gif"); 

     OutputStream o = response.getOutputStream(); 

     o.write(imgData); 

     o.flush(); 

     o.close(); 

    } catch (Exception e) { 

     out.println("Unable To Display image"); 

     out.println("Image Display Error=" + e.getMessage()); 

     return; 

    } finally { 

     try { 

      rs.close(); 

      stmt.close(); 

      con.close(); 

     } catch (SQLException e) { 

      e.printStackTrace(); 

     } 

    } 
%></td> 
     <td><button type="button" id= "Next" onclick= "next()">></button></td> 



    </tr> 

</table> 

      </td> 

    </body> 
</html> 

这里有什么错?

回答

0

,你需要做到以下几点:

月1日 - 一个了解创建servlet和在侑的servlet连接到数据库并获取图像并写入图像流。

public class ImageServlet extends HttpServlet { 

    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 

     response.setContentType("image/jpeg"); 

     byte[] imageBytes = getImageAsBytesFromDB(); 


     response.setContentLength(imageBytes.length); 

     response.getOutputStream().write(imageBytes); 


     response.getOutputStream().flush(); 
     out.close(); 

    } 

} 

2日 - 在HTML做到以下几点:

<img src="url to your servlet"> 

希望帮助