谢谢该博主https://www.cnblogs.com/chenyongjun/p/3541842.html
jsp中定义变量test
<%
String test =
"I am testing !"
; // JSP片段中定义变量
%>
js中使用刚才在jsp中定义的test
<SCRIPT LANGUAGE =
"JavaScript"
>
var tmp =
"<%=test %>"
; // 通过
"<%=javaCode %>"
引用jsp中的变量、方法等,注意:一定要加双引号
alert(tmp);
</SCRIPT >
用于将JS中变量存储到隐藏控件中
<!
-- 在form中设置隐藏控件,用来存储JS中的值 -->
<form
name
=
"frmApp"
action
=
"a.jsp"
id=
"frmAppId"
mothed=
"post"
/>
<input id=
"test"
type=
"hidden"
name
=
"test"
>
</form>
<script language=
"javascript"
>
function
setItemValue(){
var tmp =
"testing"
;
document.getElementById(
"test"
).value = tmp; // 将JS变量值存储到隐藏控件中
}
function
submit(){
var frm = document.getElementById(
"frmAppId"
); // 获取表单
frm.submit(); // 对表单进行提交
}
</script>
- 以下是a.jsp中在表单进行提交后,对变量进行接收的代码
<%
String test = request.getParameter(
"test"
); // test为隐藏控件名
%>
补充:
HttpServletRequest request = ServletActionContext.getRequest(); System.out.println("list-长度=" + list.size()); request.setAttribute("devdataList",list);
使用request.setAttribute(“key”,“value”);把数据发送到页面上