Session簡介
Session是服務端技術(shù)凯沪,利用這個技術(shù)鸠儿。利用這個技術(shù)献丑,服務器在運行時可以為每一個用戶的瀏覽器創(chuàng)建一個獨享的session對象,由于Session為用戶瀏覽器獨享轧苫,所以用戶在訪問瀏覽器資源時楚堤,可以把各自的數(shù)據(jù)放在各自的session中,當用戶再去訪問服務器的其他web資源時含懊,其他web資源再從用戶各自的session中取出數(shù)據(jù)為用戶服務。
創(chuàng)建或獲取Session衅胀,并以Cookie的方式回寫JSESSIONID岔乔。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//Session的另外一種獲取方式,這種方式只獲取不創(chuàng)建滚躯。比如查看購物車的操作雏门,如果用戶沒有購物的話,沒有必要創(chuàng)建一個Session掸掏。
// request.getSession(false);
String sessionid = session.getId();
Cookie cookie = new Cookie("JSESSIONID",sessionid);
cookie.setPath("/");
//設(shè)置cookie的保存時長為30分鐘茁影,因為Session一般就在服務器中保存30分鐘,多了也沒用丧凤。募闲。。
cookie.setMaxAge(30*60);
response.addCookie(cookie);
session.setAttribute("name","洗衣機");
}
獲取Session中的內(nèi)容
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String product = (String) session.getAttribute("name");
out.write("您購買的是:"+product);
}
把JESSIONID寫到瀏覽器的url中
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.getSession();
//添加SessionId愿待,因為cookies的SessionId可能會被用戶禁用
String url1 = response.encodeURL("/SessionDemo1");
String url2 = response.encodeURL("/SessionDemo2");
out.print("<a href="+url1+">購買</a><br/>");
out.print("<a href="+url2+">結(jié)賬</a>");
}
Session 登錄實例
首頁:
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Heinika Web</title>
</head>
<body>
歡迎你:${user.userName}<br>
<a href="login.html">登錄 </a>
<a href="/LogoutDemo">退出登錄</a>
<br/>
this my first web app
</body>
</html>
登錄頁:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>
<form action="/LoginDemo">
用戶名:<input type="text" name="username"><br/>
密碼:<input type="password" name="password"><br/>
<input type="submit" value="登錄">
</form>
</body>
</html>
登錄Servlet:
@WebServlet(name = "LoginDemo")
public class LoginDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
List<User> users = DB.getAll();
for (User user:users){
if(user.getUserName().equals(username)&&user.getPassword().equals(password)){
request.getSession().setAttribute("user",user);
response.sendRedirect("/index.jsp");
}
}
out.write("用戶名或密碼不對浩螺!");
}
}
//用于模擬數(shù)據(jù)庫
class DB{
public static List list = new ArrayList();
static {
list.add(new User("aaa","111"));
list.add(new User("bbb","222"));
list.add(new User("ccc","333"));
}
public static List getAll(){
return list;
}
}
退出登錄:
@WebServlet(name = "LogoutDemo")
public class LogoutDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
if(session == null){
response.sendRedirect("/index.jsp");
return;
}
session.removeAttribute("user");
response.sendRedirect("/index.jsp");
}
}