aspose file tools*
The moose likes JSP and the fly likes Begginer question, assigning a java string the value of a passed parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Begginer question, assigning a java string the value of a passed parameter" Watch "Begginer question, assigning a java string the value of a passed parameter" New topic
Author

Begginer question, assigning a java string the value of a passed parameter

Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Howdy.

I'd like to assign a java string within a JS function the value of the functions parameter. How would I go about doing this? This is my current code:



It doesn't seem to like that though, can anyone point out what I'm doing wrong?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

You can't do that.

Remember that the JSP code executes on the server to create an HTML/Javascript page to send to the client. So all you can do is to "write" the Javascript to be sent.

Server-side Java and client-side Javascript can not interact in any way.
[ July 16, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Yes, you've stumbled onto one of the keys issues of JSP -- an important point that once you learn it, you will never forget it. So, without further ado:

Your JSP is executed on the server-side. This means all the Java code within your JSP is finished executing by the time it reaches the client. Now, the JS code runs on the client. So you see the problem here? You cannot assign a Java variable a value based on a JS parameter, because once it's time for the JS to run, the Java code has already finished.

For example:



So, by the time the above code gets to the client, the JS function will look like this:


Try it out yourself -- write a small function like the one above, then when the JSP page comes up, do a VIEW SOURCE, and you'll see what I mean.

This was a huge wake-up call for me when I was first learning JSP. But like I said, you won't forget this point because it's pretty important.
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Apologies, let me clarify.

I'm going to be using a string I pass to a method to help build an ArrayList. I would like to convert the parameter (val) to a java recognized String to ease my using it later on.

Most of the method will be in Java, so it's essentially just getting it all into a format that that section of the method will understand.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

I'm going to be using a string I pass to a method to help build an ArrayList.


The method is a Javascript method executed on the client, right?

ArrayList is a Java class that can only exist on the server.

They cannot interact.

Perhaps you should back up a bit and explain the bigger picture of what you are trying to accomplish.
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Alright, first thing I should mention is that this is in Struts. I didn't post there because I assumed this was a JSP question.

Big picture is that I have a dynamic search list result page. Each row contains a checkbox, ID, name etc.

When a check box has been selected, I want to add the ID to an ArrayList to store it until I get back to my ActionClass where I can deal with it.

I can create the table and pass the ID as a parameter, and from there I'm stuck.
[ July 16, 2004: Message edited by: Karl Beowulph ]
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
I'm not sure if this is helpful at all, but my other idea would be to build a string consisting of the passed values seperated by a bar (2342|3343|1222|554...) which I could then pass and break-down.

I'm pretty sure I could do that entirely with javascript.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
First thing you can do, is come up with a naming convention for your checkbox fields, such as: check0, check1, ...check2. This way, your actionServlet can easily grab the checkbox parameters in a loop. Now, make sure you give the appropriate values to each checkbox. In this case, it seems all you are interested in is the ID, so, just assign each checkbox value with the value of the ID it's associated with:



Once the form is submitted, your actionServlet will iterate through a loop, checking each checkbox parameter for null. If it's not null, the checkbox has been checked, and the value of the parameter will be the record ID. This is where you can begin to build your ArrayList.
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Is there a way to dynamically name checkboxes?

Because it's the result of a search, I have no idea how many checkboxes there will be when the page is loaded (currently they are created within a nested:iterate tag.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Yes. In your tag definition, you might have something like so:


[ July 16, 2004: Message edited by: Jeffrey Hunter ]
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Alright.

After getting the ID though, how would I put that into an array?
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
In your actionServlet, which handles the form submission, you might have something as follows:



NOTE: Because you do not know the number of records, you may need to pass this value as another parameter (HIDDEN field), so your actionServlet knows how many iterations to perform.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

If you are submitting all the checkboxes together in one form, then you can just use the same name and use a setter/getter like so:



getCheckbox() should return all the values that have been checked. You should initialize the checkbox array in the ActionForm's reset() method.


Junilu - [How to Ask Questions] [How to Answer Questions]
Karl Beowulph
Ranch Hand

Joined: May 31, 2004
Posts: 130
Thanks, I'll give those a try.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Begginer question, assigning a java string the value of a passed parameter