2015-12-16 24 views
-4

當我運行此代碼,illegalException ::響應的getWriter()已被定義

illegalException ::響應的getWriter()已定義。

<% 
      Blob image = null; 
       Connection con = null; 
       byte[ ] imgData = null ; 
       Statement stmt = null; 
       ResultSet rs = null; 
       try { 
        Class.forName("com.mysql.jdbc.Driver"); 
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hicheck","root","root"); 
        stmt = con.createStatement(); 
        String s="sivabrahma"; 
        PreparedStatement ps=con.prepareStatement("select photo from hic_relation where email=? "); 
        ps.setString(1,"sivabrahma"); 
        rs=ps.executeQuery(); 
        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"); 
        response.getOutputStream(); 
        ServletOutputStream o = response.getOutputStream(); 
        o.write(imgData); 
        o.flush(); 
        o.close(); 
         System.out.println(o.toString()); 
      System.out.println(imgData.toString()); 
       } 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(); 
       } 

       } %> 

當我從檢索數據庫異常圖像發生。

+0

你的問題是不準確的。 1)例外的名稱不正確。 2)從數據庫中檢索圖像時不會發生異常:它發生在那之後。此外,有關Java異常的幫助問題應始終包含完整的堆棧跟蹤。 –

回答

0

你可以在JSP或者OutputStreamWriter不both.since JSP腳本已經有一個隱含的out,你不能叫getOutputStream()

Google文檔的getOutputStream():https://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#getOutputStream()

IllegalStateException - 如果getWriter方法已在 上調用此響應

+0

他是在'JSP'而不是在'Servlet'中完成的。 – Satya

+0

當然,我不是使用作家..! –

+0

@Satya JSP轉換爲servlet – Ramanlfc

0

您無法安全地調用response.getOutputStream()getWriter()在JSP中。 JSP已經創建了響應的編寫器並將使用它。

想一想。 JSP引擎如何知道由JSP組裝的輸出的正確交錯,並輸出直接寫入流/寫入器的JSP代碼片段(「scriptlets」)。

如果要直接寫入JSP中的響應流,請使用隱式聲明的JSP out變量...,它爲您提供了JSP使用的JSPWriter。

相關問題