1 Reply Latest reply on Jan 6, 2009 7:33 AM by 843807

    Just a little help...


      I'm new in JavaBeans, and at the vary begin I have a sort of problem, and it's about the logic insede JavaBeans, or I have an incomplete information (data) within the project I'm studying.
      This is a simple page using bean:

      <title>A dose of Dilbert</title>
      <body bgcolor="white">
      <h1>A dose of Dilbert</h1>

      <jsp:useBean id="cartoon"
      class="com.ora.jsp.beans.motd.CartoonBean" />
      <img src="images/<jsp:getProperty name="cartoon"
      property="fileName" />">


      And this is a bean(a Java's class file), where is determinated bean's logic :

      package com.ora.jsp.beans.motd;

      import java.util.*;
      public class CartoonBean implements java.io.Serializable {
      private static int index = -1;
      private List fileNames;

      public CartoonBean( ) {
      initFileList( );

      public String getFileName( ) {
      if (index > fileNames.size( ) - 1) {
      index = 0;
      return (String) fileNames.get(index);

      private void initFileList( ) {
      fileNames = new ArrayList( );

      In the "images" folder there is stored a four images, variously named (dilbert2001113293109.gif, dilbert2001166171108.gif, and so on...)
      Each time I reload the page (above) I got a different images displayed on the page. Well, it should be a logic of bean, right?
      But according to bean above, I cannot find any logic or program flow that allow an images of completely different names to be invoked (displayed)???? When bean is called, we retrieve a his getter method value, and here it is this line : " return (String) fileNames.get(index);" But no where in the code we store an images of different name in the collection but in one place?
      So where is the place where were stored a remained three images from the folder????
        • 1. Re: Just a little help...
          You are using a BEAN on one a single page without defining the scope of it. So by default the scope of bean is PAGE.
          This means when the page is loaded, there is a bean initialized and same is available to that particular page.

          Now in your bean, the method getFileName(), increases everytime the value of variable "index", and reading the name from list of that index.

          Definitely everytime you refresh the page, the value of "index" will be increased and you will get the next image_file_name.

          For better understanding, i will suggest to run the application in debug mode and put the dubug point inside your bean methods.