什么是Session:
服务器会给每一个用户(浏览器)创建一个Seesion对象;
一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
用户登录之后,整个网站它都可以访问!–> 保存用户的信息;保存购物车的信息……
…
…
Session和cookie的区别:
Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个)
Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
Session对象由服务创建;
xml中设置session的过期时间
<!--设置Session默认的失效时间-->
<session-config>
<!--15分钟后Session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>
使用SessionDemo
使用session存一个字符串
package com.myweb;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Session使用
//设置编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取Session对象
HttpSession session = req.getSession();
//给session存东西
session.setAttribute("admin","蜡笔小新");
//假定一个登陆成功页面
resp.getWriter().println("蜡笔小新登陆成功");
//获取Session的ID
String sessionId = session.getId();
//判断Session是不是新创建
if (session.isNew()){
resp.getWriter().write("session创建成功,ID:"+sessionId);
}else {
resp.getWriter().write("session以及在服务器中存在了,ID:"+sessionId);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
使用session取值
package com.myweb;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
//读取session
HttpSession session = req.getSession();
if (session.getAttribute("admin")!=null)
{
resp.getWriter().print("欢迎回来");
resp.getWriter().println(session.getAttribute("admin").toString());
}else
{
resp.getWriter().print("游客您好");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
使用session存一个object对象
demo
封装对象
package com.myweb.jobo;
//封装一个对象
public class Admin {
private String username;
private String nickname;
//有参构造
public Admin(String username, String nickname) {
this.username = username;
this.nickname = nickname;
}
//无参构造
public Admin(){
}
//get/set
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
//Object toString
@Override
public String toString() {
return "Admin{" +
"username='" + username + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
session中存对象
package com.myweb;
import com.myweb.jobo.Admin;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置字符集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
//session存一个object对象
HttpSession session = req.getSession();
session.setAttribute("admin",new Admin("abcd","蜡笔小新"));
//模拟登陆成功
resp.getWriter().print("蜡笔小心登陆成功");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
session obj取值
package com.myweb;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
String objStr = session.getAttribute("admin").toString();
resp.getWriter().print(objStr);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}