Get Session Attribute in Javascript
posted 1 year ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I want to get properties from a class that has been assigned to a Session Attribute in Javascript.
The following code will return undefined for both hospitaliteminput and hospitalitemDescription
My server code:
My JS Code:
The following code will return undefined for both hospitaliteminput and hospitalitemDescription
My server code:
My JS Code:
posted 1 year ago
indentedBillRequestParmsObj will not be a JavaScript object with properties itnbr and itdsc, it's a JavaScript string with as value the result of calling toString() on the IndentedBillRequestParms instance.
To let indentedBillRequestParmsObj be a JavaScript representation of the IndentedBillRequestParms instance you need some processing:
First convert the object to a JSON string, using a library like Jackson, Gson or if available JSON B (there are even more) Properly escape the JSON, to prevent injection attacks. This can be easily done using StringEscapeUtils.ESCAPE_JSON from Apache Commons Text
The result would then be this (replace escape and toJson to your actual methods):
The JavaScript will now contain something like this:
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Steve Dyke wrote:
indentedBillRequestParmsObj will not be a JavaScript object with properties itnbr and itdsc, it's a JavaScript string with as value the result of calling toString() on the IndentedBillRequestParms instance.
To let indentedBillRequestParmsObj be a JavaScript representation of the IndentedBillRequestParms instance you need some processing:
The result would then be this (replace escape and toJson to your actual methods):
The JavaScript will now contain something like this:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
posted 1 year ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Moved to the JSP forum. Please be sure to post JSP questions in the correct forum. Thanks.
posted 1 year ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Pædantry time again.
You don't store data in a "class", you store it in a "class instance", a/k/a an Object. Objects in a JEE HttpSession are session-scope objects.
JavaScript runs on the client. The HttpSession object and the session scoped objects named in its Map are entirely located in the server so the only way to get their values in JavaScript is to have the JavaScript request that the server return them — e.g., via AJAX,
Now if you want specifically to get the session property when the page is first rendered to the client, that's trivial:
Note that I've retrieved only a single property value, however. Java Object instances are binary components with opaque internals and cannot be used on clients at all (especially since Applets are dead now).
If you want the JavaScript equivalent of an object and some/all of its properties. you'd have to have the server return that object as a JSON string*. Meaning that the server needs to construct the JSON either manually or with the aid of a JSON library in the application.
---
* That's why they call it JavaScript Object Notation.
You don't store data in a "class", you store it in a "class instance", a/k/a an Object. Objects in a JEE HttpSession are session-scope objects.
JavaScript runs on the client. The HttpSession object and the session scoped objects named in its Map are entirely located in the server so the only way to get their values in JavaScript is to have the JavaScript request that the server return them — e.g., via AJAX,
Now if you want specifically to get the session property when the page is first rendered to the client, that's trivial:
Note that I've retrieved only a single property value, however. Java Object instances are binary components with opaque internals and cannot be used on clients at all (especially since Applets are dead now).
If you want the JavaScript equivalent of an object and some/all of its properties. you'd have to have the server return that object as a JSON string*. Meaning that the server needs to construct the JSON either manually or with the aid of a JSON library in the application.
---
* That's why they call it JavaScript Object Notation.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
| Get me the mayor's office! I need to tell him about this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |













