Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JSP/Java/JavaScript Communication

Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JavaScript function that includes Java code that needs to use varables from the JavaScript.

As you can see I have the following problems:

1. Giving a unique name to each instance of the Launcher class.
2. Passing the JavaScript "control" variable to the Java setId() method.
3. Passing "theForm.elements[i]" to the setTextComponentName() and setCallBack() methods where i is the iterator in the JavaScript loop.

Thanks for any help.
Posts: 67451
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be done. That's not the way things work.

All of the code in your JSP executes on the server in order to compose a plain-old vanilla HTML/Javascript page to send to the browser. Once it gets there, your JSP is long gone and reclaimed (a simplificiation, but close enough for now).

So by the time your Javascript function finally gets around to executing, the Java is not there -- it executed long ago in order to render the page. Take a look at the results of a View Source to see what's being sent to the browser.

The only interaction possible between the client (your Javascript) and the server (your Java) is through the normal request/response HTTP cycle.
    Bookmark Topic Watch Topic
  • New Topic