Session会话:是基于Cookie的一种会话机制,数据存放在服务器端。
- Session创建:如果有在Servlet里面调用了request.getSession()。
- Session销毁:session会话时间过期或者关闭服务器就会被销毁。设置会话时间过期时间到服务器的web.xml里设置,比如说tomcat的设置。
设置会话时间过期时间,这里用tomcat为例子:在tomcat中,默认为30分钟
Java里使用Session时使用到的方法:
- HttpSession session= request.getSession();
- 得到会话ID:String id=session.getId();
- 存数据:session.setAttribute(arg0, arg1);
- 取数据:session.getAttribute(arg0);
- 移除数据:session.removeAttribute(arg0);
- 强制让会话无效:session.invalidate();
方法使用案例:这个案例是模拟任务个数的打开,清除所有任务。当点击超链接时,通过使用session的方式将数据临时存储到服务器上,并进行下一次操作。
product_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<a href="shopServlet?id=0"><h2>aa</h2></a><br>
<a href="shopServlet?id=1"><h2>bb</h2></a><br>
<a href="shopServlet?id=2"><h2>cc</h2></a><br>
<a href="shopServlet?id=3"><h2>dd</h2></a><br>
<a href="shopServlet?id=4"><h2>ee</h2></a><br>
<a href="shopServlet?id=5"><h2>ff</h2></a><br>
</body>
</html>
shopServlet.java
package c.session;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class shopServlet
*/
public class shopServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
//1.获取要添加任务栏的id
int id=Integer.parseInt(request.getParameter("id"));
String[] names={"aa","bb","cc","dd","ee","ff"};
String name=names[id];
//2.获取任务栏存放任务的session Map<String,Integer> aa 3
//把一个map对象存放到session里面去,并且保证只保存一次
Map<String, Integer> map=(Map<String, Integer>)request.getSession().getAttribute("shop");
//session里面没有存放过任何东西
if(map==null){
map=new LinkedHashMap<String, Integer>();
request.getSession().setAttribute("shop", map);
}
//3.判断任务栏有没有该任务
if(map.containsKey(name)){
map.put(name, map.get(name)+1); //在原来的基础上+1
}else{
map.put(name, 1); //没有该任务,置为1
}
//4.输出界面(跳转)
response.getWriter().write("<a href='product_list.jsp'><h2>继续执行</h2></a><br>");
response.getWriter().write("<a href='shop.jsp'><h2>所有任务</h2></a>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
shop.jsp
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h2>您的任务栏如下:</h2>
<%
//1.先获取到map
Map<String,Integer> map=(Map<String,Integer>)session.getAttribute("shop");
//2.遍历map
if(map!=null){
for(String key: map.keySet()){
int value=map.get(key);
%>
<h3>名称:<%=key %> 数量:<%=value %></h3><br>
<%
}
}
%>
<a href="clearshopServlet"><h4>清空任务栏</h4></a>
</body>
</html>
clearshopServlet.java
package c.session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class clearshopServlet
*/
public class clearshopServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session=request.getSession();
//强制干掉会话,里面存放的任何数据都没有了 session.invalidate();
//从session中移除某一个数据
session.removeAttribute("shop");
response.sendRedirect("shop.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}