Solved

How can I select 2 random images from 20 in JSP?

Posted on 2003-11-21
12
749 Views
Last Modified: 2010-04-01
hi ...

i need to be able to select 2 images at random from approx 20 images and display them on a page.

at this point i am assuming that the simplest route will be to have the image filenames in an array and then pick 2 of them to display. any better solutions will be appreciated but i have no database access.

i will need to know how to set up the array and then select two at random and write the selected values to the HTML.

if someone could please help with some code that will sort this out for me i would be a happy bunny.

thanks!
0
Comment
Question by:equentin
  • 8
  • 4
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9797147
say all your images are in a "images" folder (under the jsp folder)

Also, assume they are all named

1.jpg -> 20.jpg

Then you can do:

<img src="images/<%= "" + (int)( Math.random * 20 ) %>">
<img src="images/<%= "" + (int)( Math.random * 20 ) %>">

But I guess you want more intelligent code than that ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797152
Oops

<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
0
 

Author Comment

by:equentin
ID: 9797221
tim, i do need to have the filenames (in an array, or as variables) and not just a number and i also need to ensure that the same random number is not duplicated.

any thoughts?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:TimYates
ID: 9797499
Stopping the same random number is easy:

int one = (int)( Math.random() * array.length ) ;
int two = one ;
while( two == one )
    two = (int)( Math.random() * array.length ) ;

As for the array of images... hmmm

are the images inside a folder in the same webapp as this jsp?

Is it just the images in that folder?
0
 

Author Comment

by:equentin
ID: 9797564
thanks ..

the images are in an images folder off the site root. all links on the site are site root relative. so all images are src="/assets/image.jpg"

there must be a way to have a list of image filenames in an array and pull out items [random x] and [random y]?

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797711
you can do:


<%!
  class MyFilter implements java.io.FilenameFilter
  {
      public boolean accept( java.io.File dir, String name )
      {
          name = name.toUpperCase() ;
          return name.endsWith(".JPG") || name.endsWith(".PNG") || name.endsWith(".GIF") ;
      }
  }
%>

<%
    String pathToTomcat = "/opt/tomcat/webapps/ROOT/" ;
    String pathToAssets = "assets/" ;

    String[] array = new java.io.File( pathToTomcat + pathToAssets ).list( new MyFilter() ) ;

    for( int i = 0 ; i < array.length ; i++ )
    {
        array[ i ] = array[ i ].substring( pathToTomcat.length() ) ;
    }

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="images/<%= array[ one ] %>">
<img src="images/<%= array[ two ] %>">
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797714
or something similar ;-)

hee hee
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797734
Sorry...that's wrong...

This should work:

----------------------

<%!
  class MyFilter implements java.io.FilenameFilter
  {
      public boolean accept( java.io.File dir, String name )
      {
          name = name.toUpperCase() ;
          return name.endsWith(".JPG") || name.endsWith(".PNG") || name.endsWith(".GIF") ;
      }
  }
%>

<%
    String pathToAssets = "/opt/tomcat/webapps/ROOT/assets/" ;

    String[] array = new java.io.File( pathToAssets ).list( new MyFilter() ) ;

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="assets/<%= array[ one ] %>">
<img src="assets/<%= array[ two ] %>">
0
 

Author Comment

by:equentin
ID: 9797821
nearly there!

actually, perhaps i should have mentioned that there are a million other images in the assets folder and that i will need to manually populate the array ...

;P
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 9797849
hahahaha....perhaps ;-)

Does that work then?  Does it pick two random assets?

Cool :-)

just make a static array then:

change the code to:

---------------------

<%
    String[] array = new String[] { "image1.jpg", "image2.gif", "blah.jpg", etc... } ;

    int one = (int)( Math.random() * array.length ) ;
    int two = one ;
    while( two == one )
        two = (int)( Math.random() * array.length ) ;
%>

<img src="assets/<%= array[ one ] %>">
<img src="assets/<%= array[ two ] %>">
0
 

Author Comment

by:equentin
ID: 9797914
you are a star ... that's perfect

thanks :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9797934
hee hee...

Glad I could help :-)

Tim
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question