Get request attributes in JavaScript

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} 

etc. However, I was never sure how to access the request parameters in JavaScript. I tried several things, for example:

 var jsAtt = ${myAtt}; var jsAtt = '${myAtt}'; var jsAtt = eval(${myAtt}); var jsAtt = eval('${myAtt}'); 

etc. but nothing works.

Is there a way to capture request attributes via JavaScript? Please do not jQuery, servlets, etc. I want clean JavaScript code.

I'm kind of amazed, I did not find this already asked. So sorry if this is a duplicate, and I just did not see the original.

+6
source share
3 answers

Using the following should work.

 var jsAtt = '${myAtt}'; 

I think I stumbled upon problems due to trying to dynamically generate a string based on my needs, which does not seem to be like JavaScript. For example, this would have problems:

 var counter = 1; var jsAtt = '${myAtt' + counter + '}'; 

JavaScript seems to recognize the syntax of the request parameter, but only if it is completely predefined.

+12
source

For GET requests, you can get values ​​from QueryString. Check out this page: http://www.thimbleopensource.com/tutorials-snippets/get-request-parameters-using-javascript

I am not sure about POST requests.

0
source

I have not tested this, but have you tried this?

 ${pageContext.request.myAtt} 
0
source

Source: https://habr.com/ru/post/900315/


All Articles