• 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

User data from java object to assign javascript variables

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I really need some help understanding how to implement a part of a small web application I'm writing.

The actual web page is really just a form which they will use to calculate some property related stuff. But in order to speed things up for the user, the first time they use the form in a session, a lot of the form's fields will be filled in with values that come from their 'user settings' which are stored in a mySql database.

I have no problem with the JDBC stuff, but what I want to do is take the user settings values and assign them to javascript variables that the webpage will then be able to use. I'm not sure how to go about doing this. I've made servlets that use info from the client and send it to a POJO to do some work and then send back info to the client, but I here I want to serve up a new page with javascript global variables, whose values have been got from the database, already assigned.

Would I have to serve up the page, and then immediately send the variables via JSON or something? It would be great if I could just somehow assign the variables to the relevant javascript file and then send the HTML file, which would then get the javascript file that I've assigned all the new values to, if you get what I mean. I'm not at all sure if I'm on the right track though.

I'd be grateful for any ideas as to how I should be going about this.

Many thanks

Joe
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to a more appropriate forum.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just act like they are plain text and set them on the html page.

var x = "PUT IT HERE";

I think you are making it more complicated than it needs to be.

Eric
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the javascript in the JSP. Do not put javascript in a separate .js file.

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric and James, thank you for your replies.

Can I just confirm, Eric, are you saying that I should do as James is suggesting and use a JSP setting the variables as he shows in his code?

I see that this way all the javascript variables will be set. Then the javascript files that are separate from the HTML / JSP (but are requested in the header as usual) will assume that these variables have been set by this JSP and won't have to declare them again.

Cheers for the code James.

Regards

Joe
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript does not care where the variables are declared.

Eric
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic