Reading a JSP variable from JavaScript

Siddiqui picture Siddiqui · Jan 26, 2011 · Viewed 220k times · Source

How can I read/access a JSP variable from JavaScript?

Answer

Jigar Joshi picture Jigar Joshi · Jan 26, 2011
alert("${variable}");

or

alert("<%=var%>");

or full example

<html> 
<head>
<script language="javascript"> 

function access(){ 
  <% String str="Hello World"; %>
   var s="<%=str%>"; 
   alert(s); 
} 

</script> 
</head> 

<body onload="access()"> 
</body> 

</html>

Note: sanitize the input before rendering it, it may open whole lot of XSS possibilities