otnj2ee
07-17-2009, 11:22 PM
In a JSP, I try to pass a Java object to a javascript function, like this:
<%
Test test = request.getSession().getAttribute("test");
//Test has a field of lasName, such as test.getLastName() will return "john"
%>
//This is for simplizing. In reality this js codes are in different file such as includeFile.js
<script>
var obj = <%= test%>;
alert(obj.lastName);
</script>
But it does not work. Then I tried:
var obj = "<%= test%>";
<c:set var="t" value="<%=test%>"></c:set>
var obj = "${t}";
But none worked.
What should I do to achieve the goal?
Thanks
Scott
<%
Test test = request.getSession().getAttribute("test");
//Test has a field of lasName, such as test.getLastName() will return "john"
%>
//This is for simplizing. In reality this js codes are in different file such as includeFile.js
<script>
var obj = <%= test%>;
alert(obj.lastName);
</script>
But it does not work. Then I tried:
var obj = "<%= test%>";
<c:set var="t" value="<%=test%>"></c:set>
var obj = "${t}";
But none worked.
What should I do to achieve the goal?
Thanks
Scott