Solved

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

Posted on 2003-11-21
12
692 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ajax auto save 15 93
Display current date,month,and year to JTextField 3 41
nested if has else if 13 88
maven webservice example issue 2 91
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now