[Solved] store value of my Java function return String In JavaScript variable [closed]


The storing of data from a JSP scriptlet should be pretty straight forward just like you have it.

var id=<%=ctx.getAttribute("id")%>

Although I would recommend you to be careful with the returning data, if it isn’t a number for sure, then you must put it between quotes just to be sure the JS doesn’t break. For example:

if <%=DataRet.get(2)%> returns a string "TEST" then the resulting JS would look like:

var id=TEST

And that would just simply break because there is no variable named TEST. You need to enclose it in double or single quotes like:

var id="<%=DataRet.get(2)%>";

Also you have to keep in mind the semicolons at the end of every line, and escape any possible chars that can break the JS code. Remember, the JS code has not been executed yet after the JSP has compiled, so it is like if you wrote that JS code manually.

If this code is not working start by checking what <%=DataRet.get(2)%> is returning, and also if there are any JS errors.

Hope it helps.

1

solved store value of my Java function return String In JavaScript variable [closed]