-
No, you can't. JavaScript is executed on the client side (browser), while the session data is stored on the server. However, you can expose session variables for JavaScript in several ways:
- a hidden input field storing the variable as its value and reading it through the DOM API
- an HTML5 data attribute which you can read through the DOM
- storing it as a cookie and accessing it through JavaScript
- injecting it directly in the JS code, if you have it inline
In JSP you'd have something like:
<input type="hidden" name="pONumb" value="${sessionScope.pONumb} />
or
<div id="product" data-prodnumber="${sessionScope.pONumb}" />
then is JS
var inputs = document.getElementsByTagName("input"), len = inputs.length, i, pONumb;
for (i = 0; i < len; i++) {
if (inputs[i].name == "pONumb") {
pONumb = inputs[i].value;
break;
}
}
or
var product = document.getElementById("product"), pONumb;
pONumb = product.getAttribute("data-prodnumber");
The inline example is the most straightforward, but if you then want to store your JavaScript code as an external resource (the recommended way) it won't be feasible.
<script>
var pONumb = ${sessionScope.pONumb};
[...]
</script>
출처 url
'2017년 > web Development' 카테고리의 다른 글
jpa를 알아보자 (0) 2017.08.10 mysql과 spring을 연동해보자 (0) 2017.08.07 웹개발환경을 구축해보자 [spring framework,maven] (0) 2017.08.07 xml과 json (0) 2017.07.05 우분투에 mysql/eclipse/tomcat 설치해서 개발환경 구축하기 (0) 2017.06.29