Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using an array from a Bean in JSP

Posted on 2007-12-02
1
Medium Priority
?
1,396 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
A solution for Fortify Path Manipulation.
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.

782 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