Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Request Attributes in JavaScript

Tags:

javascript

jsp

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.

like image 329
Snowy Coder Girl Avatar asked Oct 28 '11 19:10

Snowy Coder Girl


1 Answers

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.

like image 139
Snowy Coder Girl Avatar answered Sep 18 '22 14:09

Snowy Coder Girl