Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old September 22nd, 2004, 01:22 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thx a ton angrycat....
it finally worked..u made my day.. :)

y do u say tat it is a mistake bush...?
Reply With Quote
  #12 (permalink)  
Old September 22nd, 2004, 03:20 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont see why this is mistake.

True the page is only compiled once, but it is not static.
Building on the code I gave, what should be compiled (once only) is a DYNAMIC jsp page that receives parameters (for database queries) and returns an image based on those parameters.

The fact that jsp compiles once only (unless source is updated) does not detract from the fact they are dynamic.

ag


Reply With Quote
  #13 (permalink)  
Old September 22nd, 2004, 03:52 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

bush,

Think I know where your coming from, but still think it will work!

getImage is a jsp that queries the database, in the example I gave the the parameter a static "1".

The code needs to be developed to use a variable as the parameter, which would be passed something like this:

<td><img src="getImage.jsp?myRecordId=myParam"></td>

The variable myParam makes it possible to call getImage.jsp with different parameters, thus returning whatever image you want.

Perhaps Pallav could try it and let us know!

ag


Reply With Quote
  #14 (permalink)  
Old September 24th, 2004, 06:10 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi angrycat,

I tried wat u said (<td><img src="getImage.jsp?myRecordId=myParam"></td>) n it worked perfectly f9....

No problem at all.....

Thnx 4 the help...
Reply With Quote
  #15 (permalink)  
Old March 17th, 2006, 11:20 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default




please tell me what is myRecordId=myParam

and how should i get that recordid
<td><img src="getImage.jsp?myRecordId=myParam"></td>






Reply With Quote
  #16 (permalink)  
Old November 4th, 2006, 09:43 AM
Registered User
 
Join Date: Nov 2006
Location: chennai, , India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi pallav

can u send me your Your_Code.jsp

iam struggling to do this

iam using struts and hibernate

my requirement is also the same

displaying image from databse

to the jsp

regards
amir

Reply With Quote
  #17 (permalink)  
Old July 11th, 2007, 03:08 AM
Registered User
 
Join Date: Jul 2007
Location: Stockholm, , Sweden.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi !!!
      i would like to display an image, actually i'm getting this image dyanmically after every 5 mins being processed by some database queries, I'm getting values from the database, process them and make a jpeg chart , now i want it to be displayed on a jsp , and i want that jsp to display this image after every 5 mins. Could you please tell me how should i do it , i need to get this problem solved as soon as possible.
please help .

Quote:
quote:Originally posted by angrycat
 Got mySql solution from SunJava, would suggest you have a look there if your still stuck.

Assuming you are able to return the image into a recordset with something like:

String query = "select image from images where id =1";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
rs.next();

The code youre really after goes something like this:


byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=getimage.jpeg");
while((size=sImage.read(bytearray))!= -1 )
    {
        response.getOutputStream().write(bytearray,0,size) ;
    }

        response.flushBuffer();
sImage.close();

The problem with this is that you cannot put it into a table etc. The original poster of the solution in the Sun forum 'included' the jsp containing the above in the table of a second jsp, which is what I do also.

Reply With Quote
  #18 (permalink)  
Old November 15th, 2007, 03:38 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, i am facing problems so hope you guys can help me out! I did try out many different solutions. What i want is to retrieve the BLOB image in mysql and display it in jsp.

I tried out the code. But there is no image displayed out. I have no choice what went wrong.
I embed the servlet in one of the tags so i am not sure if i am right. If not, please guide me. Thanks.

---comments.jsp---

table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="428" height="63">
<tbody>
<td bgColor="#008080" width="28" align="center" height="19">No.</td>
<td bgColor="#008080" width="126" align="center" height="19">Name</td>
<td bgColor="#008080" width="224" align="center" height="19">Picture</td>
<td bgColor="#008080" width="270" align="center" height="19">Comments</td>


<%

String DRIVER = "com.mysql.jdbc.Driver";
Class.forName(DRIVER).newInstance();

Connection conn = null;
Statement stmt = null;

try{

String dbUrl = "jdbc:mysql://localhost/homepage?user=root&password=mysql";

int i=1;
conn = DriverManager.getConnection(dbUrl);
stmt = conn.createStatement();
String query = "Select * FROM Comments";
ResultSet rst = stmt.executeQuery(query);
while(rst.next()){

if(i==(i/2)*2){
%>

<tr>
<td width="28" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=i%></td>
<td width="126" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(3)%></td>
<td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"> <embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed></td></td>
<td width="270" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(4)%></td>
</tr>

<%
}else{
%>
<tr>
<td width="28" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=i%></td>
<td width="126" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(3)%></td>
<td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed> </td>
<td width="270" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(4)%></td>

</tr>
<% }

i++;
}
rst.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
%>
</tbody>
</table>

<---ImageServlet.java-->

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;

public class ImageServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {

String filename = request.getParameter("name");
//filename = "baby.gif";
//execute you sql to get the clob here
String url = "jdbc:mysql://localhost/homepage";
String userID="root";
String PWD="mysql";
Connection conn = null;
String classPath= "com.mysql.jdbc.Driver";

try{
Class.forName(classPath).newInstance();
conn = DriverManager.getConnection(url,userID,PWD);
}catch(Exception exc){
out.println(exc.toString());
}
Statement stmt = null;
ResultSet rst = null;

// Connection c = ...
stmt = conn.createStatement();
//String strSQL = "SELECT Picture FROM comments WHERE name='" +filename +"'";
String strSQL = "SELECT Picture1 FROM comments WHERE name='" filename"'";
rst = stmt.executeQuery(strSQL);

byte [] barray = rst.getBytes("binaryfile");
rst.close();
stmt.close();
response.setContentType("image/jpeg");
response.getOutputStream().write(barray);

} catch (Exception sqle) {

}

}

}

Reply With Quote
  #19 (permalink)  
Old December 1st, 2007, 10:30 AM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

package image;

import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.sql.*;

public class images
{
  /*-------------------------
   * Get the Blob image
   *------------------------*/
  public static byte[] getPhoto (OracleConnection conn, int iNumPhoto)
       throws Exception, SQLException
  {

    String req = "" ;
    Blob img ;
    byte[] imgData = null ;
    Statement stmt = conn.createStatement ();

    // Query
    req = "Select image From IMAGES Where ImageID = " + iNumPhoto ;

    ResultSet rset = stmt.executeQuery ( req );

    while (rset.next ())
    {
      img = rset.getBlob(1);
      imgData = img.getBytes(1,(int)img.length());
    }

    rset.close();
    stmt.close();

    return imgData ;

  }

}

 The JavaServer Page includes the bean so its methods can be accessed in the JSP page using scriplets and “photo” as a named bean reference



<%@ page import = "image.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "oracle.jdbc.OracleConnection" %>
<jsp:useBean id="photo" class="image.images" scope="session" />
<%

  int iNumPhoto ;
  oracle.jdbc.driver.OracleConnection conn = null;

  if ( request.getParameter("imgID") != null )
  {

    iNumPhoto = Integer.parseInt(request.getParameter("imgID")) ;

    try
    {
       conn = …………;
       conn.setAutoCommit (false);

       // get the image from the database
       byte[] imgData = photo.getPhoto( conn, iNumPhoto ) ;
       // display the image
       response.setContentType("image/gif");
       OutputStream o = response.getOutputStream();
       o.write(imgData);
       o.flush();
       o.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
      … Close the connexion … ;
    }
  }
%>

http://studyjava.org/forums/
Reply With Quote
  #20 (permalink)  
Old November 24th, 2008, 06:22 AM
Registered User
 
Join Date: Nov 2008
Location: bangalore, karnataka, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Pallav
 Hi angrycat,
ur solution enables me to display an image (stored as blob data type in mysql) on a jsp page..

However i cannot display the image(a gif image) in a table..

I followed ur instruction and included ur code(say Your_Code.jsp)in a second jsp page but only the image got displayed,not the table and its contents..


the sample code of second jsp page(say show.jsp) is as follows

<table>
<tr>
<td>Hello</td>
<td><%@include file="Your_Code.jsp"%></td>
                     OR
<td><jsp:include page="Your_Code.jsp" flush="true"/></td>

</tr>
</table>

the output of show.jsp is the gif image only without any html...

Kindly let me know how u implemented this functionality..

Thnx..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to display pictures on JSP page? Kisat JSP Basics 1 November 3rd, 2008 09:02 PM
JSP page not display data coming from socket ashvinm JSP Basics 0 February 20th, 2008 02:10 AM
Display many pictures NBM Java GUI 0 September 3rd, 2006 06:54 AM
How to display Vietnamese Unicode on JSP page? minhpx JSP Basics 4 May 18th, 2006 11:16 PM



All times are GMT -4. The time now is 12:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.