This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes accessing values of context attribute in jquery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "accessing values of context attribute in jquery" Watch "accessing values of context attribute in jquery" New topic
Author

accessing values of context attribute in jquery

stephen r davidson
Ranch Hand

Joined: Jun 23, 2013
Posts: 58
I have the following code in MyContextListener.java

String []lookupAccountRegion = {"one","two","three"};
servletContext.setAttribute("case.account.region__c",lookupAccountRegion);

and am trying to use the variable as the SOURCE in an autocomplete statement in my jsp/javascript.
Just for testing, I try to access the context attribute like this:

var lookupValues2 = $("case.account.region__c");

and my javascript terminates here.

If I remove my attempted reference to the context attribute, and add a variable in the javascript like this:
var lookupValues = ["this", "that", "other"];

then I can use lookupValues in my source statement and autocomplete works.

So it doesn't work with the array I am passing in....

I know this is likely some woodenhead moment on my part, but wonder if you can suggest a path forward?



J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 865
    
  13

Those context attributes are on the server. Javascript runs on the client and has no concept of application context.

You have a couple of options. Make an Ajax call to a servlet that will retrieve those attributes and send them to the client. Or store them in the jsp as hidden fields and then the Javascript can retrieve them when the html reaches the client.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Unless they need to be submitted as part of a form later, I wouldn't use hidden fields. Just create a JavaScript variable that contains the value.

E.g.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
stephen r davidson
Ranch Hand

Joined: Jun 23, 2013
Posts: 58
Hmm. Bear. Are you saying that I CAN access my context attribute with a javascript variable?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

No. I'm saying that you can create the markup for a JavaScript variable in the JSP.

Remember JSP is processed on the server. It will make the substitution, and what will be sent to the browser would be:

There is no access from JavaScript. None. Nada. Nil. It's just text substitution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing values of context attribute in jquery
 
Similar Threads
dynamic appending of javascript from javascript
Jquery Autocomplete for remote Database is not working
How to remove cached suggestions on a text field
form/input tags and turning off autocomplete
static initializer block