Meaningless Drivel is fun!*
The moose likes JSP and the fly likes How to retrieve images using JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to retrieve images using JSP" Watch "How to retrieve images using JSP" New topic
Author

How to retrieve images using JSP

Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
how to retrive the image with description from database using jsp
Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
how to retrive the image with description from database using jsp..........
i need full source code i am using netbeans 6.9.1 and my sql server 2012 database (management studio) please help any body can help............
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

Sabarish Jaganathan wrote:how to retrive the image with description from database using jsp

JSP has no part in this. You put images into a page using the <img> tag.

If the image is not a file you can reference by URL, and must be generated, then the URL to the image can be to a servlet that serves the image data. But the image is still created on the page using an <img> tag.

i need full source code

That's not how things work here. You write the code and we help you with it. Please read NotACodeMill.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%String id=request.getParameter("id");
try{Connection con;
InputStream sImage;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vishvum");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT image FROM test WHERE id = '"+id+"'");
if(rs.next()){
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
}
con.close();
}
catch(Exception ex){
out.println("error :"+ex);
}
%>

in this above jsp code i am getting this error
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
how to resolve it please help me
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

That code should not be in a JSP. No how, no way!

Not only is a really bad practice to put any Java code in a JSP page, a JSP is for creating the HTML view and there's nothing you can do with the image data in the JSP.

As I already pointed out: use an <img> tag in the JSP to reference a servlet that serves up the image data. Be sure to set the response headers accordingly.

P.S. In the future, please be sure to UseCodeTags when posting code. Thanks.
Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
how to call Servlet file from jsp page in netbeans 6.9.1
Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
what is meant by mapping ? how to do mapping for web.xml and servlet
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

What does Netbeans have to do with it? It's just the editor.

In any case, set up a mapping like you would for any other servlet and use the URL as the image source.
Sabarish Jaganathan
Greenhorn

Joined: Apr 05, 2013
Posts: 14
ok what type of url !!! you mean servlet file location in the computer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61456
    
  67

If you don't know how to write, declare, map and invoke a servlet, then you'll need to read the tutorial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve images using JSP