Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

Submitting a Javascript array into a Javabean/JSP

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
bmartins
Asked:
bmartins
  • 4
  • 2
  • 2
  • +1
1 Solution
 
cheekycjCommented:
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
 
cheekycjCommented:
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
 
kennethxuCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bmartinsAuthor Commented:
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
 
kennethxuCommented:
>> 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
 
cheekycjCommented:
I thought you were trying to create a Java array from a JavaScript array.  Isn't that the case?

CJ
0
 
bmartinsAuthor Commented:
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
 
cheekycjCommented:
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
 
girionisCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now