通过url传参
这是我觉得比较简单的传参方式了,两个页面之间传参,第一个页面通过一个链接,传递希望传递的参数并跳转到另一个页面,另一个页面通过内置对象request来获得这个参数
页面a.jsp代码
<a href = b.jsp?num=1>1</a>
这是html代码,我们在链接中写了一个url,其中num=1是我们希望传递的参数
页面b.jsp代码
<%
String s = request.getParameter("num");
int n = Integer.parseInt(s);
%>
这样我们就通过一个超链接将num=1的参数传递给页面b.jsp
通过表单form传递参数
页面a.jsp代码
<form action="b.jsp" method="get" align="center">
<input type="text" name="name"/>
<input type = "submit" name = "submit" value = "提交" />
</form>
页面b.jsp代码
<%
String s = request.getParameter("name");
int n = Integer.parseInt(s);
%>
页面b.jsp还是利用jsp内置对象request获取参数
表单form还可以实现将上一个页面的数据传给下一个页面,即数据在三个页面中传递
例如我希望在页面一获取用户的账号密码,在页面二获取用户的用户名,然后在页面三展示出来。这时候我利用表单来做就很简单
a.jsp
<form action="b.jsp" method = "get" align="center">
账号:<input type = "text" name="account" />
密码:<input type = "password" name="password" />
<input type = "submit" name = "submit" value="登录"/>
</form>
b.jsp
<% String account = request.getParameter("account"); %>
<form action="c.jsp" method="get" align="center">
姓名:<input type="text" name="name" />
//最关键是这一句,将a.jsp中的账号信息传递给了c.jsp
<input type=hidden name="account" value=<%=account %>>
<input type="submit" value="提交"/>
</form>
c.jsp
<%
String name = request.getParameter("name");
String account = request.getParameter("account");
%>
account:<%=account %>
name:<%=name %>
通过动作元素<jsp:param>以及<jsp:include>
a.jsp
<jsp:include page="b.jsp">
//value的值不管是什么类型都要加引号
<jsp:param name="count" value="" />
</jsp:include>
b.jsp
<%
String s = request.getParameter("count");
int count = Integer.parseInt(s);
%>
第一篇博客,很多不足,请多多指教