• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. In your tag definition, you might have something like so:


[ July 16, 2004: Message edited by: Jeffrey Hunter ]
 
Karl Beowulph
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright.

After getting the ID though, how would I put that into an array?
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Karl Beowulph
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll give those a try.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic