利用Cookie进行两个页面的传值,CookieP1.jsp显示一个数的平方,Cookie显示这个输的立方,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'CookieP1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//定义一个变量
String str="12";
int number=Integer.parseInt(str);
%>
该数的平方为:<%=number*number %><hr>
<%
//将str存入Cookie对象
Cookie cookie=new Cookie("number",str);
//设置Cookie的存活期为 600秒
cookie.setMaxAge(600);
//将Cookie对象保存于客户端
response.addCookie(cookie);
%>
<a href="CookieP2.jsp">到达CookiesP2.jsp</a>
</body>
</html>
CookieP2.jsp,详细代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'CookieP2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//从Cookie获得number
String str=null;
Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("number")){
str=cookies[i].getValue();
break;
}
}
int number=Integer.parseInt(str);
%>
该数字的立方为:<%=number*number*number%><hr>
</body>
</html>
点击上图到达CookisP2.jsp:
response和jsp动作指令进行跳转页面的对比:练习
responseTest1.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'requestTest1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="responseTest2.jsp">
输入学生的姓名:<input type="text" name="stuname">
<input type="submit" value="查询">
</form>
</body>
</html>
responseTest2.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'responseTest2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//第一种方式跳转页面
//response.sendRedirect("responseTest3.jsp");
%>
/*第二种方式跳转页面*/
<jsp:forward page="responseTest3.jsp"></jsp:forward>
</body>
</html>
首先利用jsp动作进行跳转页面:
点击查询:
地质栏地址不变
数据能够共享
把responseTest2.jsp里面的代码更换
点击查询:
发现地址栏改变,数据不能共享