如何在javascript中获取java变量值??

liukui359 2009-08-09
比如我现在在JSP的JAVA中定义了以个变量,我如何在javascript中获取到这个变量的值?
<%
   String guser = (String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER);
%>

<script language="javascript">

function checkAll(user)
{
  var gusername = "<%=user%>";   //此处的gusername 压根没有获取到
  alert(gusername);
}
上面这种方法不行

然后我试用传参
<%
   String guser = (String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER);
%>

<script language="javascript">

function checkAll(user)
{
  var gusername = user;
  alert(gusername);
}

<body>
<input type="button" value="<liferay-ui:message key="confirm" />" onClick="checkAll("<%=guser%>")">
</body>
仍然不行,请教高手们,该如何是好啊
yjrl 2009-08-10
先在页面上放歌input为hidden,然后把java变量放入之后用JavaScript获取
istone 2009-08-10

liukui359 写道
   String guser = (String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER);

取到值了吗?

在js里直接用el语句取session里面的值不行吗?
liukui359 2009-08-10
取到了,方法如下:
<%
   String guser = (String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER);
%>

<script language="javascript">

function checkAll()
{
  var gusername = document.form1.user.value;
  alert(gusername);
}
<form name="form1">
<body>
<input type="button" value="<liferay-ui:message key="confirm" />" onClick="checkAll()">
</body>
<input type= "hidden" name ="user" value="<%= guser%>" />
</from>
grrr28 2009-08-10
var guser='<%=(String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER)%>'
shawnfree 2009-08-11
<%
   String guser = (String)request.getSession().getAttribute(CASFilter.CAS_FILTER_USER);
pageContext.setAttribute("guser",guser);
%>
<script language="javascript">
var gusername = "${guser}";
  alert(gusername);
</script>
seagulls88 2009-08-11
<input type="button" value="<liferay-ui:message key="confirm" />" onClick="checkAll(<%=guser%>)"> 就可以了。
seagulls88 2009-08-11
不用 搞得那么麻烦啊。
liukui359 2009-08-12
seagulls88 写道
<input type="button" value="<liferay-ui:message key="confirm" />" onClick="checkAll(<%=guser%>)"> 就可以了。


非常感谢,这样也是可以的,原来是我多用了引号onClick="checkAll("<%=guser%>")">,

alanwu 2010-01-20
题外话,java6可以让applet和javascript直接通信  呵呵
Global site tag (gtag.js) - Google Analytics