简体   繁体   中英

How to access Java session object through JavaScript stored outside of JSP

I am storing a variable through java in session by following command :

request.setAttribute("metricValues", metricDataList);   

Now I am trying to access this session object through a java script which is stored outside the JSP.

On your JSP create a hidden HTML element with this value.

When you body has loaded use javascript or jquery to read this value.

Java

session.setAttribute("metricValues", metricDataList);   // you state session

JSP

<input id='mv' type='hidden' value='${sessionScope.metricValues}'/>  // you state session variable

JS

$(document).ready(function(){
    var mv = $('#mv').val ();

You could add this values in http header on server side and read them with javascript on client side ? I mean you could do that in bean or in phaselistener ?

var session;
$.ajaxSetup({cache: false})
$.get('getsession.jsp', function (data) {
    session = data;
});

AND jsp will be:

<% response.getWriter().write(request.getAttribute("metricValues"));   %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM