session.setAttribute("CONNECTION_DBNAME", dbName);
Use bracket notation to get an object's value by a variable key, e.g. obj[myVar] . The variable or expression in the brackets gets evaluated, so if a key with the computed name exists, you will get the corresponding value back. Copied!
Since JS code runs on the user's browser and Java runs on your server, the only way to pass data from Javascript to Java is by doing an Ajax call. On your server, you need a API endpoint which accepts requests, then after receiving the desired data, you can handle it on your Java program.
LiveConnect is a technique that allows Java and JavaScript to communicate with each other. It allows your Java class to call JavaScript methods and access the JavaScript environment. JavaScript can also access Java objects and invoke methods on them.
First access the variable in scriptlet.
<%
String param= (String)session.getAttribute("CONNECTION_DBNAME");
%>
Then use like this.
<script>
var X = '<%=param%>';
</script>
then you can access the name using x.
<script>
<%
String dbname=(String)session.getAttribute("CONNECTION_DBNAME");
%>
</script>
this code is usefull to you..
You can use hidden element in JSP to get the value from session like:-
<textarea id="txtData" style.display='none'><%=session.getAttribute("CONNECTION_DBNAME") %></textarea>
afterwards you can get the value in your javascript by var dbConnName=document.getElementById("txtData").value;
and you are done.
Scriptlets were OUTLAWED more than a decade ago!
A better way... in jsp, include the values in a hidden div:
<div id="javaValues" style="display: none;">
<div id="employee">${employee}</div>
<div id="dept">${dept}</div>
</div>
Use <div>
's rather than <input type="hidden">
, since they will not interfere with your form-postings.
In javascript (assuming jQuery) you can then access the values, for example:
var employee = $("#employee").html().trim();
var dept = $("#dept").html().trim();
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