Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get string values from Request and Session  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my jsp, I have some javascript, is it possible for me to get string variables set in Http Request and Session in my javascript ? If yes, can you show me the syntax ? Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not with JavaScript. JavaScript is a client-side mechanism.

That's what JSP is for -- to inject server-side values into the HTML page before it gets sent to the browser.

If you need the value on the client, then you use JSP to create the JavaScript markup to capture the data. For example:

This gets evaluated on the server and the HTML page with the value injected is what is sent to the browser.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may benefit from reading this article.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, don't really get how you tied the jsp variable value to javascript var.



Can you elaborate the above syntax ? If I ahve jsp varible
<% String s = request.getParamater("ABC"); %>

How do I assign this "s" to a javascript variable ?

 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an expression in the JSP Expression Language (commonly known as EL). Are you not using EL in your JSP programming? The answer to your specific question is this:

Don't omit the quotes; you want this to produce something like

in the HTML you're generating.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!