Solved

Using an array from a Bean in JSP

Posted on 2007-12-02
1
1,357 Views
Last Modified: 2013-11-24
I am attempting to learn JSP to rewrite our company website.  One thing that we would like to do is be able to show customers their recent activity on their account.  For a demo I have been trying to create a servlet that loads 10 numbers into a bean, and then a JSP that pulls those numbers back out.  I have a working JSP with a single value but haven't been able to figure out how to get the array out.

The way I am currently thinking of using the servlet and the bean are below.  
Sample Servlet code:
 

for(int i = 0;i<10;i++){

    myBean.setNextNumber(i)

}

_______________________________________________________
 

Sample bean:
 

public void setNextNumber(int i){

    Numbers[i] = i;

}
 

public int getNextNumber(int i){

    return Numbers[i];

}

________________________________________

JSP code to retrieve

??

Open in new window

0
Comment
Question by:VoodooFrog
1 Comment
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 20391488
I suggest that you use conventions. See
http://www.it-careernet.com/itc/javabeantechnology.htm#Indexed%20Properties    
http://java.sun.com/docs/books/tutorial/javabeans/properties/indexed.html  
In your bean you should use,  
       private int[] numbers;
       public int getNumbers(int index){
                                                            return this.numbers[index];
       }
       public void setNumbers(int index, int number){
                                                                                this.numbers[index] = number;
       }
       public int[] getNumbers(){
                                                return this.numbers;
       }
       public void setNumbers(int[] numbers){
                                                                     this.numbers = numbers;
       }
In your servlet use ,
                    MyBean myBean = new MyBean();
                    for(int i = 0;i<10;i++){
                                            myBean.numbers[i] = i;
                    }
                    request.setAttribute("myBean", myBean);  
    or you could use session scope
                    session.setAttribute("myBean", mybean);
In your JSP use  
                ${myBean.numbers[0]}
                ${myBean.numbers[1]}
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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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

20 Experts available now in Live!

Get 1:1 Help Now