?
Solved

Submitting a Javascript array into a Javabean/JSP

Posted on 2003-02-27
10
Medium Priority
?
658 Views
Last Modified: 2007-12-19
Hi,

in my jsp i have a bidimensional array. Let's call it myArray[i][j]. I have also a form and i need to submit this array to other jsp or a javabean where i insert in three database tables (one for each dimension in  the array and the third one to make the join). I'm going to use <input type="hidden"> and the myArray.join('$') function to submit the array value. My problem is how to program the bean and how to call it from the jsp page (using the jsp:usebean tag, maybe?) to do the insertion.

Can anybody help me?
Is the way i'm going to do this the best way?
Shouldn't i use Javabeans or should i use another approach?

Thank you so much for your help.
35 points is all i have. Sorry!

Best regards,
Bruno Martins
0
Comment
Question by:bmartins
[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
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8035636
I think the approach you are taking is fine.  Using a hidden field with concatenated values will work.

as far as the bean goes, pass the concatenated data to your bean like so:

String 2dArrayField = request.getParameter("hiddenFieldName");

com.mycompany.MyBean mb = new com.mycompany.MyBean();
mb.parseArrayData(2dArrayField);

your bean will look something like this:

package com.mycompany;

import java.util.*;

class MyBean implements java.io.Serializable {
 // bean's 2d array
 int [][] arr;
 // default constructor
  public MyBean() { }
  /*
   *  This method assumes that the data is in the form of "arr[1,1],arr[1,2],arr[1,3]$arr[2,1],arr[2,2]...."
   */
  public parseArrayData(String data) {
    if (data != null) {
      StringTokenizer st = new StringTokenizer(data,"$");
      int i=0;
      while (st.hasMoreTokens()) {
        String outerArrayStr = st.nextToken();
        StringTokenizer innerArrayStr = new StringTokenizer(outerArrayStr,",");
        int j=0;
        while (innerArrayStr.hasMoreTokens()) {
          arr[i][j++] = Integer.parseInt(innerArrayStr.nextToken());
        }
        i++;
      }
    }
  }
}

This assumes that your array is an array of ints.

for strings it is a simple change.

CJ
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 140 total points
ID: 8035664
one thing you will have to change:
// change the 5's to whatever your max size will be
int[][] arr = new int[5][5];

CJ
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8036087
if both jsp are under same application, you can store the array in session, you can then retrieve it from other jsp pages. let me know if you need more detailed information.
0
Independent Software Vendors: 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!

 

Author Comment

by:bmartins
ID: 8037122
Both jsp are in the same application.
My approach wasn't adding it to the session because i thought (i'm new to this) that it wasn't very "nice" to do so. That's because the array will be there as long as the session is valid even in other jsp where it is useless. Please correct me if i'm wrong and give me more detailed information about how to add it to the session and retrieve it from another jsp.

By the way, does session has somekind of limit so that we cannot add more parameters?

Best regards,
Thanks cheekycj, kennethxu.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8037190
>> as long as the session is valid even in other jsp where it is useless.
that's the drawback, but you can remove it after use and session will expire.

>> By the way, does session has somekind of limit so that we cannot add more parameters?
No, or limited by server memory.

to put any object into session:
session.setAttribute( "myName", myObject );

to retrieve it from session
MyClass myObject = (MyClass) session.getAttribute( "myName" );

to remove it after use:
session.removeAttribute( "myName" );

MyClass -- type of the object;
myObject -- the object you want to put in, array is one kind of object, so you can put/get whole array in one statement.
myName -- the name you give to your stored object, you use this name to retrieve your object back.

for arrays:
session.setAttribute( "myName", myArray );
MyClass[][] myArray = (MyClass[][]) session.getAttribute
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8037233
I thought you were trying to create a Java array from a JavaScript array.  Isn't that the case?

CJ
0
 

Author Comment

by:bmartins
ID: 8037526
Yes, but from a jsp.

Will this work from a jsp ?
<%
  com.mycompany.MyBean mb = new com.mycompany.MyBean();
  mb.parseArrayData(2dArrayField);
%>

Don't i/shouldn't i have to use <jsp:usebean> tags ?
Is it good policy inserting into the database from that bean 'MyBean'?

Thank you all.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8037664
yes that should if you are posting javascript array as a hidden form field called 'hiddenFieldName'

if you want to store it the info in the DB create another Method in MyBean:

public void insertIntoDB() {
  // this method should iterate over arr[][] and insert into the db.
}

to retrieve the array add this method:
public int[][] getArr() {
  return arr;
}

<%
 String 2dArrayField = request.getParameter("hiddenFieldName");
 com.mycompany.MyBean mb = new com.mycompany.MyBean();
 mb.parseArrayData(2dArrayField);
 mb.storeIntoDB();
%>

to pass the array from one jsp to another you can store it in session. so store it like so:

<%
session.setAttribute("arrayInSession", mb.getArr());
%>

retrieve it using
<%
int [][] arr = (int[][])session.getAttribute("arrayInSession");
%>

I am not sure about the casting above.. maybe kennethxu can help verify that.

CJ
0
 
LVL 35

Expert Comment

by:girionis
ID: 9990328
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to cheekycj

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 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