一、区别
对象名称 | 生存周期(就是保存值时间) | 保存的位置 | 用在什么地方 |
session | 在项目web.xml中设置,一般都设置30分钟。 如果不操作当前页面,30分钟后清除。 | 服务器端 | 填写表单,页面传递参数 |
application | 服务器重启,就没了 | 服务器端 | 计数器,所有用户共享变量 |
cookie | 自己设定或手动清理cookie | 在手机里的硬盘里 | 用户自动登陆 |
二、session示例
1、login_1.jsp(参数发送页)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>LOGIN登陆页</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">
</head>
<body>
<% session.setAttribute("user","dxnn520"); %>
<p>接收登陆信息如下: </p>
<input name="注册" type="button" id="btn1" title="登注册" value="开始传参到index_2.jsp页" onclick="location.href='index_1.jsp?name=张飞&sex=男'" />
</body>
</html>
2、index_1.jsp(参数接收页)
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JAVA - 主页面</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">
</head>
<body>
<p>接收登陆信息如下: </p>
登录名:
<%
String m_user=(String)session.getAttribute("user");
out.println(m_user); // 输出变量
%>
<br> <br>
</body>
</html>
3、如何设置session生存时间,注意:30是秒
三、cookie示例
1、cookie用法表
类型 | 方法名 | 方法解释 |
String | getComment() | 返回cookie中注释,如果没有注释的话将返回空值. |
String | getDomain() | 返回cookie中Cookie适用的域名. 使用getDomain() 方法可以指示浏览器把Cookie返回给同 一域内的其他服务器,而通常Cookie只返回给与发送它的服务器名字完全相同的服务器。注意域名必须以点开始(例如.yesky.com) |
int | getMaxAge() | 返回Cookie过期之前的最大时间,以秒计算。 |
String | getName() | 返回Cookie的名字。名字和值是我们始终关心的两个部分,笔者会在后面详细介绍 getName/setName。 |
String | getPath() | 返回Cookie适用的路径。如果不指定路径,Cookie将返回给当前页面所在目录及其子目录下 的所有页面。 |
boolean | getSecure() | 如果浏览器通过安全协议发送cookies将返回true值,如果浏览器使用标准协议则返回false值。 |
String | getValue() | 返回Cookie的值。笔者也将在后面详细介绍getValue/setValue。 |
int | getVersion() | 返回Cookie所遵从的协议版本。 |
void | setComment(String purpose) | 设置cookie中注释。 |
void | setDomain(String pattern) | 设置cookie中Cookie适用的域名 |
void | setMaxAge(int expiry) | 以秒计算,设置Cookie过期时间。 |
void | setPath(String uri) | 指定Cookie适用的路径。 |
void | setSecure(boolean flag) | 指出浏览器使用的安全协议,例如HTTPS或SSL。 |
void | setValue(String newValue) | cookie创建后设置一个新的值。 |
void | setVersion(int v) | 设置Cookie所遵从的协议版本。 |
2.login_1.jsp(写Cookie)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>LOGIN登陆页</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">
</head>
<body>
<%
// 写入cookie
Cookie cookie= new Cookie("user","dxnn520");
cookie.setMaxAge(60);
response.addCookie(cookie);
cookie = new Cookie("password","8384");
response.addCookie(cookie);
%>
<input name="注册" type="button" id="btn1" title="登注册" value="写入并读取Cookie" onclick="location.href='index_1.jsp'" />
</body>
</html>
3.index_1.jsp(读取cookie页)
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JAVA - 主页面</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">
</head>
<body>
<%
// 读取cookie
Cookie m_cookie[] = request.getCookies();
String m_user="";
String m_password="";
for(int i = 0;i < m_cookie.length;i++)
{
if(m_cookie[i].getName().equals("user"))
{
m_user=m_cookie[i].getValue();
// request.setAttribute("user",m_cookie[i].getValue());
// out.print(m_cookie[i].getValue());
}
if(m_cookie[i].getName().equals("password"))
{
m_password=m_cookie[i].getValue();
// request.setAttribute("password",m_cookie[i].getValue());
// out.print(m_cookie[i].getValue());
}
}
%>
<p>从COOKIE中读取信息如下: </p>
登录名: <% out.print(m_user); %> <br> <br>
密 码: <% out.print(m_password); %>
<br> <br>
</body>
</html>
四、application示例
1、login_1.jsp(写入application)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>LOGIN登陆页</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">
</head>
<body>
<%
application.setAttribute("m_city", "北京");
application.setAttribute("m_date", "2020-10-01");
%>
<input name="注册" type="button" id="btn1" title="登注册" value="写入并读取application" onclick="location.href='index_1.jsp'" />
</body>
</html>
2、index_1.jsp(读取application)
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JAVA - 主页面</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">
</head>
<body>
<p>从application中读取信息如下: </p>
城 市:<%=application.getAttribute("m_city") %> <br> <br>
日期:<%=application.getAttribute("m_date") %>
<br> <br>
</body>
</html>