Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

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

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
equentin
Asked:
equentin
  • 8
  • 4
1 Solution
 
TimYatesCommented:
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
 
TimYatesCommented:
Oops

<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
<img src="images/<%= "" + (int)( Math.random * 20 ) %>.jpg">
0
 
equentinAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TimYatesCommented:
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
 
equentinAuthor Commented:
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
 
TimYatesCommented:
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
 
TimYatesCommented:
or something similar ;-)

hee hee
0
 
TimYatesCommented:
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
 
equentinAuthor Commented:
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
 
TimYatesCommented:
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
 
equentinAuthor Commented:
you are a star ... that's perfect

thanks :)
0
 
TimYatesCommented:
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.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now