1.利用javabean
Javabean类:
package entity;
public class User {
private String username="";
private String gender="";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public User() {
}
}
传参数的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<jsp:useBean id="user" class="entity.User" scope="session" />
<center>
<h1>传参页面</h1>
</center>
<hr>
<%
user.setUsername("绅士");
user.setGender("男");
%>
<center>
点击我,<a href="receive.jsp">跳转</a>
</center>
</body>
</html>
接收参数的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
<hr>
<jsp:useBean id="user" class="entity.User" scope="session"></jsp:useBean>
<p>使用JSP动作接收传参</p>
<h4>
性别:<jsp:getProperty name="user" property="username" /><br> 密码:<jsp:getProperty
name="user" property="gender" /><br>
</h4>
<hr>
<p>使用JSP普通方式接收参数</p>
<h4>
性别:<%=user.getUsername()%><br> 密码:<%=user.getGender()%><br>
</h4>
</center>
</body>
</html>
测试结果:
传参页面:
接收参数页面:
2.绑定到session对象
传参页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<hr>
<%
session.setAttribute("username", "绅士");
session.setAttribute("gender", "男");
%>
<center>
<a href="receive.jsp">传递参数</a>
</center>
</body>
</html>
接收参数页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
out.print("姓名:"+session.getAttribute("username"));
%>
<br/>
<%
out.print("性别:"+session.getAttribute("gender"));
%>
</center>
</body>
</html>
由于测试结果都成功了,博主就不上图了
3.绑定到application
传参页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<hr>
<%
application.setAttribute("username", "绅士");
application.setAttribute("gender", "男");
%>
<center>
<a href="receive.jsp">传递参数</a>
</center>
</body>
</html>
接收参数页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
out.print("姓名:"+application.getAttribute("username"));
%>
<br/>
<%
out.print("性别:"+application.getAttribute("gender"));
%>
</center>
</body>
</html>
4.绑定到request对象
这里用采用的是请求转发的方式
传参页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<hr>
<%
request.setAttribute("name","绅士");
%>
<jsp:forward page="receive.jsp"/>
</body>
</html>
接收参数页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
out.println("传递过来的参数是:"+request.getAttribute("name"));
%>
</center>
</body>
</html>
测试结果:
因为是请求转发所以页面很快的速度跳转到了接收参数的页面,所以运行结果没有显示传递参数的内容,但是浏览器的地址是传递页面的地址
表明参数已经成功传过去了
5.使用JSP动作指令传参
传参页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<%
String username="绅士";
String gender="男";
%>
<hr>
<jsp:forward page="receive.jsp">
<jsp:param name="name" value="Jakc" />
<jsp:param name="gender" value="man" />
</jsp:forward>
</body>
</html>
接收参数页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
out.print("姓名:" + name);
%>
<br />
<%
out.print("性别:" + request.getParameter("gender"));
%>
</center>
</body>
</html>
这里采用的也是请求转发的方式
6. 表单传参
传参页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<form action="receive.jsp" method="get" align="center">
姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br />
密码:<input type="password" name="password" size="20" value=""
maxlength="20"><br /> <br /> <input type="submit"
name="submit" value="登录"> <input type="reset" name="reset"
value="重置"><br />
</form>
</body>
</html>
接收参数页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
out.print("姓名:" + name);
%>
<br />
<%
out.print("性别:" + request.getParameter("password"));
%>
</center>
</body>
</html>
7.URL传参
传参页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
</center>
<hr>
<%!
String username="绅士";
String gender="男";
%>
<center>
<a href="receive.jsp?username=<%=username %>&gender=<%=gender%>">传递参数</a>
</center>
</body>
</html>
接收参数页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%
String name=request.getParameter("username");
out.print("姓名:"+name);
%>
<br/>
<%
out.print("性别:"+request.getParameter("gender"));
%>
</center>
</body>
</html>
结果:
传参页面:
接收参数页面:
URL传参的时候还有一个特别的用处,是当你在JSP页面中使用for循环输出某些信息的时候,URL传参的独特之处就体现出来了
假设我使用for循环5次,每次浏览器输出点击我跳转,并同时向session绑定参数(方便起见就用循环变量吧)
像这样:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
<%
for (int i = 0; i < 5; i++) {
session.setAttribute("index", i);
%>
<a href="receive.jsp">点击我跳转 </a><hr>
<%
}
%>
</center>
</body>
</html>
接收页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
接收到的顺序:<%=session.getAttribute("index") %>
</center>
</body>
</html>
没测试之前误以为点击不同的连接会传递不同的参数,但是事实上却是每一次穿过来的参数都是4,这就很奇怪!
为什么是这样呢?
这里博主做出解释:因为session要等到输出所有的内容,也就是说等到递增到4之后,才将最后一个i的值传给下一个页面(也就是说,i=0,时候,session绑定的对象的值是0,i=1的时候,绑定的对象的值是1,…….)
但是呢要等到i递增到4之后整个JSP页面输出完毕之后才传到下一个JSP页面,所以每一次传到去的参数就是4,接收到的参数自然每一次都是4了!
那我们要怎么做才能做到当我点击不同的连接的时候传递参数是其对应的参数呢??(比如说我们点击第一个链接的时候,传递过去的参数是0;第二个参数的时候,参数是1….)
这个时候使用URL传参就很好用了
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>参数传递页</title>
</head>
<body>
<center>
<h1>传参页面</h1>
<%
for (int i = 0; i < 5; i++) {
%>
<a href="receive.jsp?index=<%= i %>">点击我跳转 </a><hr>
<%
}
%>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>接收参数页</title>
</head>
<body>
<center>
<h1>接收参数页面</h1>
<hr>
<%--接收参数的时候必须使用这个方法否则会找不到传递过来的参数 --%>
使用request.getParameter接收到的参数 接收到的顺序:<%=request.getParameter("index")%>
<hr>
使用request.getAttribute接收到的参数
<%=request.getAttribute("index")%>
</center>
</body>
</html>
这里请注意了,一定要使用request.getParameter(string name)这个方法才能得到参数,否则是不能得到参数的!
这里给出使用不同方法获取传递过来的参数的结果,