When loading a page, I have the following in my controller:
request.setAttribute("myAtt", "Testing");
I want to access this in my JSP file. In the HTML section, I am familiar with using things like:
${myAtt} ${requestScope.myAtt}
and the like. However, I've never been sure how to access request parameters in JavaScript. I've tried a number of things, such as:
var jsAtt = ${myAtt};
var jsAtt = '${myAtt}';
var jsAtt = eval(${myAtt});
var jsAtt = eval('${myAtt}');
etc, but nothing seems to work.
Is there a way to grab request attributes via JavaScript? Please no jQuery, servlets, etc. I want pure JavaScript code.
I am kind of amazed I didn't find this already asked. So sorry if it is a duplicate and I just didn't see the orignal.
Using the following should work.
var jsAtt = '${myAtt}';
I think I stumbled across issues because of trying to dynamically generate the string based off my needs, which JavaScript seems to not like. For example, this would have issues:
var counter = 1;
var jsAtt = '${myAtt' + counter + '}';
JavaScript seems to recognize the request parameter syntax, but only if it's completely predetermined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With