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
}
}