Solved

Using an array from a Bean in JSP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 92
groupSum6 challenge 6 77
servlet example issue 6 33
arguments to jar 5 17
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

862 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

23 Experts available now in Live!

Get 1:1 Help Now