JavaScript向JSP传值
使用JavaScript通过在表单里加入隐藏域信息,然后用表单提交的方式把数据传递给JSP程序
Example(1)
代码
<script type="text/javascript" language="javascript">
function f(){
document.forms["form1"].test.value =document.forms["form1"].cloze.value+ " hello"; //获取jsp中表单的值
document.form1.submit();
}
</script>
<body>
<%
String s=new String();
String t=request.getParameter("test");
if(t!=null){
%>
你填写的值:<%=t %><br>
<%} %>
<form name="form1" method="post" action="regist.jsp">
<input type="text" name="cloze" />
<input type="hidden" name="test" value=<%=s %> />
<input type="button" value="submit" onclick="f()" />
</form>
</body>
function f(){
document.forms["form1"].test.value =document.forms["form1"].cloze.value+ " hello"; //获取jsp中表单的值
document.form1.submit();
}
</script>
<body>
<%
String s=new String();
String t=request.getParameter("test");
if(t!=null){
%>
你填写的值:<%=t %><br>
<%} %>
<form name="form1" method="post" action="regist.jsp">
<input type="text" name="cloze" />
<input type="hidden" name="test" value=<%=s %> />
<input type="button" value="submit" onclick="f()" />
</form>
</body>
Example2:
代码
1、test.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%
String s1="Hello ";
String s2="World!!";
%>
<script language="JavaScript">
function insertclick(){
var1 ="<%=s1 %>";
document.forms["insertForm"].mc.value = var1 + document.forms["insertForm"].mc.value ;
document.insertForm.submit();
}
</script>
<form name="insertForm" method="post" action="get.jsp">
<input type="hidden" name="mc" value="<%=s2 %>">
<input type="button" value="提交" onclick="insertclick()">
</form>
2、get.jsp
<%
String strVar1=request.getParameter("mc");
out.print(strVar1);
%>
//此代码参考CSDN博客:http://blog.csdn.net/xyk0830/archive/2006/08/15/1067724.aspx
<%@ page contentType="text/html; charset=gb2312" %>
<%
String s1="Hello ";
String s2="World!!";
%>
<script language="JavaScript">
function insertclick(){
var1 ="<%=s1 %>";
document.forms["insertForm"].mc.value = var1 + document.forms["insertForm"].mc.value ;
document.insertForm.submit();
}
</script>
<form name="insertForm" method="post" action="get.jsp">
<input type="hidden" name="mc" value="<%=s2 %>">
<input type="button" value="提交" onclick="insertclick()">
</form>
2、get.jsp
<%
String strVar1=request.getParameter("mc");
out.print(strVar1);
%>
//此代码参考CSDN博客:http://blog.csdn.net/xyk0830/archive/2006/08/15/1067724.aspx