Solved

Using an array from a Bean in JSP

Posted on 2007-12-02
1
1,365 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 58
mapAB Challlenge 35 145
eclipse java  build path 6 57
ForLoop Example 3 49
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

777 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