Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector to Array conversion with maintenance of elements

 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I hope someone can help with my problem. Within my JSP I am firstly returning a db query in the form of a Vector:

<% java.util.Vector myresults = dbBean.getRecords("SQL here");%>

As I already have presentation logic to deal with the returned data when it is stored within a 2D Javascript array ("an array of arrays"), what I am looking to do is to convert the data in my Vector to an array type.

Here is my try block:

try
{
if(myresults !=null)
{
String result = "";
result = (String) myresults.toString();
out.println(result);
}
}

This works to an extent as it prints the following onto the JavaScript area (where my try block is located) of my page:

[[a1, a2, a3, a4], [b1, b2, b3, b4]]

However, for the Javascript to recognise this as an array (of arrays) I believe it should look like this:

[["a1", "a2", "a3", "a4"], ["b1", "b2", "b3", "b4"]]

Firstly, am I right in this thinking?

Secondly, can anyone help achieve what I am trying to do? (Maybe there is a better way of doing this?)

Any input here is greatly appreciated.

Cheers,
Chad.

EDIT: subject edited
[ March 12, 2006: Message edited by: Chad Saviola ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad,
You need to loop through your array and ouput it in the form required by Javascript.

Alternatively if you control the Vector, you can loop through the elements and add the quotes that way.
 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeanne,

Many thanks for the reply; is there any chance you could elaborate on your suggestion please? I understand what you are saying from a desgin perspective, but how would I go about implementing this?

Do you mean I should loop thru the Vector, pull out each element into String types, and then use these strings to increment an empty array delcared in Javascript?

If so, then you are implying that there is no way to print out my Vector in the form of the following directly?

[["a1", "a2", "a3", "a4"], ["b1", "b2", "b3", "b4"]] <-- notice the quotes.

Look forward to hearing from you,

Cheers,
Chad.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chad Saviola:
Do you mean I should loop thru the Vector, pull out each element into String types, and then use these strings to increment an empty array delcared in Javascript?

It actually looks you have a Vector of Vectors of Strings. So you would use nested loops. Loop through the outer Vectors to get the inner vectors. Then loop through the inner vectors to get the Strings. So something liks this (assuming Java 5 syntax):

You also have to add the brackets and commas in the right places in the code. (With the commas watch for the special case of the first/last element.)


If so, then you are implying that there is no way to print out my Vector in the form of the following directly?

[["a1", "a2", "a3", "a4"], ["b1", "b2", "b3", "b4"]] <-- notice the quotes.

Unfortunately not. The toString() method only provides one implementation and it isn't the one you need.

There is another alternative. You can call the toString() method and alter its output. This would mean looping through to check each character and add a quote in the right places. (Personally, I think this is harder to do than the approach described above.)

Either way, note that you will want to do this in a Java class, rather than the JSP. This is more complex code.
 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Many thanks for your input on this. I think I have managed to implement your first suggestion, whereas your second suggestion may have in fact been what I was trying to acheive but seems to complex!!

I didn't think of putting this piece of code in its own Java class, though. I was under the impression that this code (within the <% ... %> tags) gets compiled before the browser client recieves the JSP, effectively being processed at the same time a Java class would, thus not making a difference?

If you could clarify that, it would be great, however, your help on the orginal question has been greatly appreciated

Cheers,
Chad.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like:



[ March 12, 2006: Message edited by: ak pillai ]
[ March 12, 2006: Message edited by: ak pillai ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need an object-relational mapping tool such as Hibernate
 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks AK & Tony. I feel I have now got this working. (Unfortunately, I have no idea what Hibernate is and within my timescales I would not be able to learn how to use it).

However, can anyone explain, as per my previous post, why it is advisable to place this code in its own Java class opposed to within the JSP?

Its only a little piece of code, wouldnt need to be abstracted out into a class for other use, and (i think) is compiled before the client recieves the page anyway, much like a class.

Thanks again for your help,

Chad.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@tony - no offence, but as this is the beginner section, and Chad's question reveals that he still has to build up a solid understanding of loops, arrays and the java-api, i'd say that the "one-line-post"-recommendation of Hibernate or any other ORM tool is far beyond the scope of this beginner forum. maybe that's even discouraging for him.

@chad - forget about hibernate for the next time, but very good point in your last post. in fact, if you try to raise your code to next advanced level, you'll soon figure out that you want to split the view of your date (the jsp) from the place where the data is assembled (controlled).

we have a common architecture for it, it's named "model-view-controller pattern". thousands of pages in the web describe this pattern way better then i could, so consider having a look here ....

or google yourself.

hope it helps,
jan
 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jan,

Thanks for your reply; it kinda clears things up - I've come across the MVC pattern a few times now, so I'll take a look into that further.

...So in conclusion, it is best to move the little bit of code away from the JSP into a class, the reasons for which will (hopefully) be revealed once I read up on the MVC. (I hope this is correct )

Thanks a lot for everyones help on this topic; you guys have been really helpful and also quick with responses!

Cheers,
C. Saviola
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey chad,

here come a few reasons, sorry for overreading it the first time. they all kind of play together.

- seperations of concerns: you want one responsibility per class. the jsp is responsible for the view, that's enough.

- maintainability: when something changes, you want to change your code in as few places as possible. when the view changes, you change the jsp. when the logic changes, you change the controller. and when the data structure changes, you want to change the model.

- reusability: when you develop complex logic on the jsp, chances are fairly low that you can ever reuse it in case you need it again somewhere else. when you have a controller-method getAllProductsInStateX(), you can call from whereever you want.

- robustnest: the less you mix responsibilities, the better for bugfixing. when your jsp looks shitty on a internet-explorer, you have to debug the html-logic. you do not want to work yourself through dozens of lines of database related code, for your jsp it's enough to know that getAllProductsInStateX() returns a list of to-be-displayed products.

- testability: you can test your code way easier and separately when you can isolate different parts. testing getAllProductsInStateX() should be fairly easy, while testing a jsp-page with a table of all products on it can be pretty adventurous.

i'm sure there is a lot more...

hope it helps,
jan
 
Chad Saviola
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan,

That definately clears things up; I now understand completely

As a newcommer to JSP (and Java in general really), I appreciate the fact that you broke it down into not technical lingo. Fantastic.

Cheers!
Chad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic