session保存服务器中
当用户发送一个HTTP请求到服务器时,服务器会检查该请求是否包含session标识符(通常是存cookie),如果没有,则会创建一个新的session(存储区域),并将session标识符发送给客户端。浏览器再发送请求时会携带session标识符,此时服务器根据session标识符就可以找到对应的session(存储区域)
使用session的时候一般要开启cookie如果浏览器没有开启cookie功能,我们可以通过html的url传参完后session的使用
没有大小的限制
信息的保存也是以键值对的形式存在的
1)是什么
是一个域对象,保存在服务器
2)特点
①键值对
②可设置过期时间
③失效:过期或关闭浏览器
request.getSession(true); //返回HttpSession
setAttribute(key,value); //设置属性 key ,value
getAttribute(key); //获取key的值
removeAttribute(String key) //通过key值删除数据
invalidate(); //将session中的变量全部清空
setMaxInactiveInterval(int interval); //设置session失效时间,单位为秒
getMaxInactiveInterval();//获取session失效时间
getId(); //获取sessionID
package cn.fpl.servlet;
import cn.fpl.pojo.User;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Date;
public class SetSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//获得sessionid
String sessionId = httpSession.getId();
System.out.println("sessionId:" + sessionId);
User user = new User();
user.setUsername("张5丰");
user.setBirthday(new Date());
user.setSex("1");
//往session中添加属性
httpSession.setAttribute("user", user);
//设置session的失效时间
httpSession.setMaxInactiveInterval(60*5);
}
}
package cn.fpl.servlet;
import cn.fpl.pojo.User;
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;
import java.util.Date;
public class GetSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//获得session中的属性
User user = (User) httpSession.getAttribute("user");
System.out.println(user);
}
}
<servlet>
<servlet-name>setSession</servlet-name>
<servlet-class>cn.fpl.servlet.SetSessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>setSession</servlet-name>
<url-pattern>/setSession</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getSession</servlet-name>
<servlet-class>cn.fpl.servlet.GetSessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getSession</servlet-name>
<url-pattern>/getSession</url-pattern>
</servlet-mapping>
setSession
控制台输出存入的sessionid
getSession
控制台输出存入的session的信息
package cn.fpl.servlet;
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;
import java.util.Date;
public class DestroySessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得session
HttpSession httpSession = req.getSession();
//session失效
httpSession.invalidate();
}
}
<servlet>
<servlet-name>destroySession</servlet-name>
<servlet-class>cn.fpl.servlet.DestroySessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>destroySession</servlet-name>
<url-pattern>/destroySession</url-pattern>
</servlet-mapping>
修改url为销毁session后,再次进行查询session信息会发现,已经查不到了!