Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Sending Javascript variables to JSP variables

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys-

I'm not sure if this is more of a Javascript issue, or a JSP issue, but I'll post it here first and see where it takes me.

I am working on a project that involves overlaying information over a google maps interface. Because google is so awesome, they have some functions built in to the API that will make my life a lot easier if I can figure out how to integrate them into normal Java variables used on the JSP page.

This is the flow of events I'd like to use:

1) JSP: get data from MySQL database
pass some of the data to Javascript google maps construction
2) JS: Use that data to run google maps functions that will return JS data
3) JSP: modify the data from JS and add it to the database

Could someone give me some pointers on how to accomplish the variable passing?

Thanks for any help!
 
Marshal
Posts: 25463
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In step 1, you run a JSP. It generates some HTML which contains some Javascript and sends it to the browser. That's it for the JSP, it has done its work and it is finished.

In step 2, the browser runs your Javascript and does all that neat Google Maps stuff.

Now your question appears to be about step 3. You want the Javascript to update the database? Remember that the JSP is finished, it's out of the picture, and anyway it was running on the server and not in your browser.

So you need your Javascript to send requests (AJAX would be useful here) to the server, where there will be something (hopefully a servlet and not a JSP) which receives those requests and uses them to update the database.
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP I would like to use incorporates some Java code on the backend that updates and accesses the database.

But, that Java code needs to be passed some parameters from the calling code that will define what needs to be added to the database. Those parameters will come from the Javascript, and I figure it's probably easier to go from JS -> JSP -> Java than it is to write the listener to get the HTTP requests from JS that would be necessary to pass the values from JS -> Java.

If I'm on the wrong track, could someone point me in the right direction?

Thanks!
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sam Gardener:
The JSP I would like to use incorporates some Java code on the backend that updates and accesses the database.

Java in a JSP is considered a poor practice and is inherently not callable. You should refactor this code out into Java classes.

I figure it's probably easier to go from JS -> JSP -> Java

As Paul pointed out, JSPs should not be targets of requests. Servlets should be. Then it would be an easy matter for your servlet to invoke the Java that you factored out of the JSp in step 1.

If I'm on the wrong track, could someone point me in the right direction?

See above.
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java in a JSP is considered a poor practice and is inherently not callable. You should refactor this code out into Java classes.



Sorry, I misspoke. My code is set up in this way.

So, by the servlet, did you mean the Java class? Is it possible for my Javascript to call a standard Java class, or do I need to write some sort of intermediary to handle the Javascript -> Java "barrier".

I'm sorry if this went off on a random tangent - I am a javascript novice and just trying to guess at the right progression of events here.

Thanks for the help!
 
Paul Clapham
Marshal
Posts: 25463
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I tried to explain earlier, your Javascript is running in the browser. It can't call anything on the server directly. If you want it to send information to the server then you have it send an HTTP request to a servlet there.
 
Bear Bibeault
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't call any random Java class. That's what servlets are for.

(That said there's a project called DWR that uses Ajax and servlets to make it look like you can directly call Java, but it's using the same mechanisms that we're talking about here).
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic