Session
2016-03-24 21:11
323 查看
除了使用cookie记录用户信息,还会使用Session来记录,Session是运行在服务器端的,当客户在访问服务器的时候,服务器会把客户的信息通过Session记录在服务器上。
用javaWeb书中话就是:如果说cookie是通过检查客户端身上的通行证来确定用户身份,那么Session就是通过检查服务器上面的客户明细表来确定身份。Session相当于程序在服务器端为客户建了一份档案,存在客户明细表中。
Session就是Java 中的HttpSession类,每个用户就是一个Session 的对象。用户的所有信息都保存在这个Session 中,这个Session就是用户在第一次请求时在服务器端创建的,和cookie也是通过字典的方式存储用户信息的。
实现登陆信息代码:
session.jsp:
<%@page
import="java.util.Date"%>
<%@page
import="java.text.SimpleDateFormat"%>
<%@page
import="java.text.DateFormat"%>
<%@page
import="com.session.Person"
%>
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
DateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
%>
<%
//保存三个数据信息
Person[] persons={
new Person("likang","123456",24,dateformat.parse("1992-01-11")),
new Person("liuming","123456",23,dateformat.parse("1993-02-22")),
new Person("liangxi","123456",21,dateformat.parse("1995-05-21")),
};
String message="";
if(request.getMethod().equals("POST")){
for(Person person:persons){
//如果用户名和密码都正确
if(person.getName().equalsIgnoreCase(request.getParameter("username"))&&person.getPassword().equals(request.getParameter("password"))){
//登陆成功,将用户信息和登陆时间保存在Session中
session.setAttribute("person", person);
session.setAttribute("logintime",
new Date());
//跳转到欢迎页面
response.sendRedirect(request.getContextPath()+"/welcome.jsp");
return;
}
}
message="登陆失败";
}
%>
<!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>Insert title here</title>
</head>
<body>
<div
align="center">
<fieldset>
<legend>登陆</legend>
<form
action="session.jsp"
method="post">
<table>
<tr>
<td>用户名:</td><td><input
name="username"
type="text"/></td>
</tr>
<tr>
<td>密码:</td><td><input
name="password"
type="password"/></td>
</tr>
<tr>
<td></td><td><input
type="submit"
value="提交"></td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
welcome.jp:
<%@page
import="java.text.DateFormat"%>
<%@page
import="java.text.SimpleDateFormat"%>
<%@page
import="com.session.Person"
%>
<%@page
import="java.util.Date"%>
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
DateFormat datefotmat=new SimpleDateFormat("yyyy-MM-dd");
%>
<%
Person person=(Person)session.getAttribute("person");
Date logintime=(Date)session.getAttribute("logintime");
%>
<!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>Insert title here</title>
</head>
<body>
<fieldset><legend>个人信息</legend>
<table>
<tr>
<td>您的姓名:</td><td><%=person.getName()
%></td>
</tr>
<tr>
<td>登陆时间::</td><td><%=logintime
%></td>
</tr>
<tr>
<td>您的年龄:</td><td><%=person.getAge()
%></td>
</tr>
<tr>
<td>您的生日:</td><td><%=person.getBrithday()%></td>
</tr>
</table>
</fieldset>
</body>
</html>
还有一个Person的实体类:
package com.session;
import java.util.Date;
public class Person {
private String
name;
private String
password;
private
int age;
private Date
brithday;
public Person(String
name,String password,int
age,Date brithday){
this.name=name;
this.age=age;
this.password=password;
this.brithday=brithday;
}
public String getName() {
return
name;
}
public
void setName(String name) {
this.name =
name;
}
public String getPassword() {
return
password;
}
public
void setPassword(String password) {
this.password =
password;
}
public
int getAge() {
return
age;
}
public
void setAge(int
age) {
this.age =
age;
}
public Date getBrithday() {
return
brithday;
}
public
void setBrithday(Date brithday) {
this.brithday =
brithday;
}
}
session的生命周期:
session一般保存在服务器的内存中的,这样会提高获取的速度。每一个用户第一次访问服务器都会创建一个Session对象,由于session是放在服务器内存中的,所以一般session不能放大量的用户信息。
session实在用户第一次访问时创建,在有效期登陆时,session都会活跃一次,记录用户的登陆信息,和登陆时间等。
session的常用方法:
setAttribute(String attribute,object value)设置session的属性;
getAttribute(String attribute)返回session的属性值;
Enumeration getAttributeNames 返回session所有的属性名;
removeAttribute 移除某个属性;
getId 返回session的ID;
getCreateionTime 返回session的创建时间;
getLastAccessed 返回session最后一次活动时间;
setMaxInactiveInterval 设置session的有效期;
invalidate 使session失效;
由于session是保存在服务器端的,对浏览器时透明的,所以需要cookie作为标示符就是向浏览器返回session的id慵懒判断是不是登陆过;
以上是我在学习session中总结的,如果有什么问题,欢迎大家提出来;
用javaWeb书中话就是:如果说cookie是通过检查客户端身上的通行证来确定用户身份,那么Session就是通过检查服务器上面的客户明细表来确定身份。Session相当于程序在服务器端为客户建了一份档案,存在客户明细表中。
Session就是Java 中的HttpSession类,每个用户就是一个Session 的对象。用户的所有信息都保存在这个Session 中,这个Session就是用户在第一次请求时在服务器端创建的,和cookie也是通过字典的方式存储用户信息的。
实现登陆信息代码:
session.jsp:
<%@page
import="java.util.Date"%>
<%@page
import="java.text.SimpleDateFormat"%>
<%@page
import="java.text.DateFormat"%>
<%@page
import="com.session.Person"
%>
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
DateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
%>
<%
//保存三个数据信息
Person[] persons={
new Person("likang","123456",24,dateformat.parse("1992-01-11")),
new Person("liuming","123456",23,dateformat.parse("1993-02-22")),
new Person("liangxi","123456",21,dateformat.parse("1995-05-21")),
};
String message="";
if(request.getMethod().equals("POST")){
for(Person person:persons){
//如果用户名和密码都正确
if(person.getName().equalsIgnoreCase(request.getParameter("username"))&&person.getPassword().equals(request.getParameter("password"))){
//登陆成功,将用户信息和登陆时间保存在Session中
session.setAttribute("person", person);
session.setAttribute("logintime",
new Date());
//跳转到欢迎页面
response.sendRedirect(request.getContextPath()+"/welcome.jsp");
return;
}
}
message="登陆失败";
}
%>
<!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>Insert title here</title>
</head>
<body>
<div
align="center">
<fieldset>
<legend>登陆</legend>
<form
action="session.jsp"
method="post">
<table>
<tr>
<td>用户名:</td><td><input
name="username"
type="text"/></td>
</tr>
<tr>
<td>密码:</td><td><input
name="password"
type="password"/></td>
</tr>
<tr>
<td></td><td><input
type="submit"
value="提交"></td>
</tr>
</table>
</form>
</fieldset>
</div>
</body>
</html>
welcome.jp:
<%@page
import="java.text.DateFormat"%>
<%@page
import="java.text.SimpleDateFormat"%>
<%@page
import="com.session.Person"
%>
<%@page
import="java.util.Date"%>
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
DateFormat datefotmat=new SimpleDateFormat("yyyy-MM-dd");
%>
<%
Person person=(Person)session.getAttribute("person");
Date logintime=(Date)session.getAttribute("logintime");
%>
<!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>Insert title here</title>
</head>
<body>
<fieldset><legend>个人信息</legend>
<table>
<tr>
<td>您的姓名:</td><td><%=person.getName()
%></td>
</tr>
<tr>
<td>登陆时间::</td><td><%=logintime
%></td>
</tr>
<tr>
<td>您的年龄:</td><td><%=person.getAge()
%></td>
</tr>
<tr>
<td>您的生日:</td><td><%=person.getBrithday()%></td>
</tr>
</table>
</fieldset>
</body>
</html>
还有一个Person的实体类:
package com.session;
import java.util.Date;
public class Person {
private String
name;
private String
password;
private
int age;
private Date
brithday;
public Person(String
name,String password,int
age,Date brithday){
this.name=name;
this.age=age;
this.password=password;
this.brithday=brithday;
}
public String getName() {
return
name;
}
public
void setName(String name) {
this.name =
name;
}
public String getPassword() {
return
password;
}
public
void setPassword(String password) {
this.password =
password;
}
public
int getAge() {
return
age;
}
public
void setAge(int
age) {
this.age =
age;
}
public Date getBrithday() {
return
brithday;
}
public
void setBrithday(Date brithday) {
this.brithday =
brithday;
}
}
session的生命周期:
session一般保存在服务器的内存中的,这样会提高获取的速度。每一个用户第一次访问服务器都会创建一个Session对象,由于session是放在服务器内存中的,所以一般session不能放大量的用户信息。
session实在用户第一次访问时创建,在有效期登陆时,session都会活跃一次,记录用户的登陆信息,和登陆时间等。
session的常用方法:
setAttribute(String attribute,object value)设置session的属性;
getAttribute(String attribute)返回session的属性值;
Enumeration getAttributeNames 返回session所有的属性名;
removeAttribute 移除某个属性;
getId 返回session的ID;
getCreateionTime 返回session的创建时间;
getLastAccessed 返回session最后一次活动时间;
setMaxInactiveInterval 设置session的有效期;
invalidate 使session失效;
由于session是保存在服务器端的,对浏览器时透明的,所以需要cookie作为标示符就是向浏览器返回session的id慵懒判断是不是登陆过;
以上是我在学习session中总结的,如果有什么问题,欢迎大家提出来;
相关文章推荐
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 杰奇登录后的东西都是在session里面的
- ASP中SESSION无法保存问题的解决办法
- Oracle中的Connect/session和process的区别及关系介绍
- Node.js编程中客户端Session的使用详解
- 浅谈COOKIE和SESSION区别
- 解析PHP的session过期设置
- php中Session的生成机制、回收机制和存储机制探究
- php中将一个对象保存到Session中的方法
- 深入解析Session是否必须依赖Cookie
- 对比分析php中Cookie与Session的异同
- PHP Session机制简介及用法
- 新手菜鸟必读:session与cookie的区别
- 深入讲解PHP Session及如何保持其不过期的方法
- PHP Session变量不能传送到下一页的解决方法
- PHP中的session永不过期的解决思路及实现方法分享
- php session实现多级目录存放实现代码
- *.ashx文件不能访问Session值的解决方法
- ASP.NET中Session和Cache的区别总结