Solved

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

Posted on 2003-11-21
12
722 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Limiting the number of characters a user can type in a text field 6 54
spring JDBC Template example error 26 236
tomcat startup error 5 59
throw exception 21 43
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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