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

how to retrieve jsp variable in javascript  RSS feed

 
shahin Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to access jsp variable in javascript i have using hidden variabel but its not working can any one help me in this regard
Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Scott Selikoff
author
Bartender
Posts: 4087
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, you shouldn't access JSP variable in javascript directly. Theoretically you could create a cookie in the JSP that the javascript can then access, but I wouldn't recommend that under most conditions. If the data really needs to be hidden, I'd suggest another pattern such as calling a servlet separately.

What I would recommend is just generate code that creates a javascript variable such as 'var myJavaScriptVar = <%=myJSPVar%>;' or some variation that could then be used by the HTML/JavaScript page.
 
shahin Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My string contains some html links like
"safdsf <a class=\"styleClass\" href = \"javascript:somemethod(HERE I HAVE NEED SINGLE QOUTE)\"";

if i include single quote thewe i won't get the value in the script if not i get the value easily
but to process further i need single qoute there Can any one help how to reslove this problem
Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you building HTML strings using Java from within a JSP page?
 
shahin Khan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either from java or from html there will be problem with single qoutes when the string contains it
I have resolved it by giving \\\" in place of ' and it works fine for me
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shahin Khan:
Either from java or from html there will be problem with single qoutes when the string contains it
I have resolved it by giving \\\" in place of ' and it works fine for me


JSP eliminates the need to build complicated, nested strings like that.
Why are you still doing it the hard way?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!