Session

除了使用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中总结的,如果有什么问题,欢迎大家提出来;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的体育馆管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本体育馆管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此体育馆管理系统利用当下成熟完善的SpringBoot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线选择试题并完成答题,在线查看考核分数。管理员管理收货地址管理、购物车管理、场地管理、场地订单管理、字典管理、赛事管理、赛事收藏管理、赛事评价管理、赛事订单管理、商品管理、商品收藏管理、商品评价管理、商品订单管理、用户管理、管理员管理等功能。体育馆管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:体育馆管理系统;SpringBoot框架;Mysql;自动化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值