Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using an array from a Bean in JSP

Posted on 2007-12-02
1
Medium Priority
?
1,393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 28

Accepted Solution

by:
rrz earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.

722 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