2014-01-25 8 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> 

뭐가 잘못 되었나요? 서블릿을 craete 및을 yor 서블릿에서 데이터베이스에 연결하고 이미지를 얻고 이미지 스트림 쓰기 -

1 :

답변

0

는 다음을 수행해야합니다.

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"> 

희망을

관련 문제