• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display Images dynamically to a servlet

 
Sunil Ravipati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw postings earlier on this topic and adopted the suggestions given , but I can display only a single image.This works for a single image jpg .All the files uploaded are
into c:\temp. The Servlet program parses through the dir and picks up
only jpg's (since I want to display all jpg's first and then expand to
gif's).The servlet now displays only an icon but no images .
I initially brought all the images in to a ArrayList but I thought its
better to display directly from the filesystem.
Please let me know what is the problem

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.* ;
/**
*
*
*
*
*
* @author
*/
public class DisplayImage extends HttpServlet
{
java.util.List aList = new ArrayList();

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


String path= "c:\\temp\\";
File f = new File(path);
String[] dirList = f.list();

int l=100;
int k=100;
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
PrintWriter out = response.getWriter();

for( int i = 0; i < dirList.length; ++i )
{
String fExt= "jpg";
// System.out.println(">>The files in the dir are>>:"+dirList[i] );
String filename = dirList[i].toString();
// System.out.println("FileName is"+filename);
int sLength = filename.length();
int sLastIndex = filename.lastIndexOf(".",sLength);

//System.out.println(">>Last Index Value>>"+sLastIndex);

String imageExt = filename.substring(sLastIndex+1,sLength);
//System.out.println(">>ImageExtensionis>>"+imageExt);
if(imageExt.equalsIgnoreCase(fExt))

{
String imagefilename = path+filename;
//System.out.println(">>Final filename is>> "+imagefilename);
Image myImage = new ImageIcon(imagefilename).getImage();

BufferedImage outImage = new BufferedImage(10,10 ,BufferedImage.TYPE_INT_RGB);
Graphics2D d2g = outImage.createGraphics();


d2g.drawImage(myImage, l, k, null);
//AffineTransformOp afo = new AffineTransformOp(af,1);
//BufferedImageFilter bf = new BufferedImageFilter(afo);

//d2g.drawImage(myImage, null, l,k);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(outImage);




k+=100;

}

else
{
System.out.println(">>Not a jpeg file>>");
}

}


out.close();

}
}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing up servlet functions with GUI functions in a single class. Exactly what are you trying to do? Where do you want the image(s) to be displayed?
Bill
 
Sunil Ravipati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I intend to do is :
1.Displa.jsp calls a servlet DisplayImage
2.DisplayImage goes to a known directory in the filesystem and picks up all images.
3.Display all the images on the servlet at once in thumbnail fashion.
4.Click on each image to enlarge it.
So far I am stuck at step 3.
So how do I proceed about on this problem if I am mixing up.
Any help is appreciated.
Thx
 
Sunil Ravipati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William,
Which is a better way to display?
Thx
 
Sunil Ravipati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not clean up code, there is a PrintWriter class which needs to be commented, since I was not getting result i though of doing with HTML <IMG/> tag.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We seem to have a conceptual problem here:
1.Displa.jsp calls a servlet DisplayImage
2.DisplayImage goes to a known directory in the filesystem and picks up all images.
3.Display all the images on the servlet at once in thumbnail fashion.
4.Click on each image to enlarge it.

Servlets don't display images. Servlets serve web pages and other resources in response to client requests. If you are going to program web applications you have to keep this in mind at all times - every servlet result is in response to a client request. Display takes place on the client browser.
Look at the HTML for a page that contains images - every image that is displayed comes from a separate request in an IMG tag that has a SRC attribute. If you make an HTML page that has an IMG tag where the SRC addresses a servlet, that servlet can serve the image data so the client browser can display it. One request can only serve one image.
Bill
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with everything that William is saying.
Basically I think you can acheive your aim with 2 servlets as follows:
First servlet generates a dynamic HTML page that contains and <img> tag for each image in your directory. The href of these <img> tags points to your 2nd servlet (e.g. second servlet listens on '/images' the href for could be something like href='images/image_name1.gif')
The second servlet would then receive the requests for the individual images and could generate the thumbnails and serve the image byte[]. (The name of the requested image can be retrieved from the request.getPathInfo() method I think)
Hope this helps.
Andy Bowes
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
go on with andy's answer, you can specify the size of the image in the <img> tag, so that thumbnail are generated. & on its click event you can call other brower window to show the image in original size.
hth
MB
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welll... specifying the size in the IMG tag doesn't really create a thumbnail because the browser has to download the whole image, then scale it to specified size so you don't gain anything.
Java does have the image manipulating capabilities to generate a true thumbnail (smaller image file) at the server and send that - but it has been quite a while since I did any image manipulation and I think a whole new API is now available.
Bill
 
Sunil Ravipati
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William, Andy , and Malhar.I appreciate all of you for the response.As mentioned I did a concept mistake of trying to rendering images on the servlet.Graphics2D API has the image manipulation capabilities.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!