• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Get Session Attribute in Javascript

 
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
  • 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:
     
    Sheriff
    Posts: 67752
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Moved to the JSP forum. Please be sure to post JSP questions in the correct forum. Thanks.
     
    Saloon Keeper
    Posts: 28325
    210
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic