Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Read multiple images from folder that's in the server java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a java web app on a wildfly server, and in a different folder on this server there are multiple photos that my app needs to read.

They're employee photos and each one has the employee ID as the name. In my app I read all employee info from the database (except the photo) and they are grouped into different departments.

I need to read the certain employee photos (from the server folder) according to the employee ID's that are in certain departments and display them in a JSP (where the rest of the employee info is displayed).

Can anyone help with how I am best doing this in a servlet? I am able to read one sample photo using the code below but am struggling to read more than one-

Below is the code from my servlet that handles the employee info (the empID's are in the employees arraylist)

Do I load all images from the folder in a servlet and then in my jsp (where I get the empID) handle the linking of the photo with the empID? Or do I handle this all in a servlet? (Sorry I am very lost and new to this)
 
Saloon Keeper
Posts: 7181
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that the directory containing the images is not directly accessible via the web server, you would need a servlet that streams the images. It would take the employee ID as a parameter, and use that to construct the path to the image file. Your doGet method is already mostly there, except for the parameter.
 
Derv McDermott
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Thank you - that was what I thought I'd do (pass the employee as the path) but do I need a loop to loop through the employee arraylist and get the image for each employee?
 
Tim Moores
Saloon Keeper
Posts: 7181
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need to get only the image you intend to serve. Whether you need to loop through all employees depends on how the image file names are arranged. If the images are arranged by ID you'll know the image path just by looking at the ID. If, OTOH, the file name is dependent on some other piece of information that is associated with the employee, you may have to loop through them in order to find the image path.
 
Derv McDermott
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will be serving more than one image though that's where im running into my problem. On the JSP there is the details of multiple employees and their images need to be beside their relevant info.
 
Tim Moores
Saloon Keeper
Posts: 7181
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet only reads a single image in one invocation. You would use it as the image's src multiple times, one for each employee.
 
reply
    Bookmark Topic Watch Topic
  • New Topic