書接上文,學完了filter之後,對java web已經有了必定的瞭解。可是,還有一些web編程必須接觸的東西,如cookie、session。如今就聊聊cookie。html
cookie是存儲在本地瀏覽器的鍵值對。衆所周知,http是無狀態的通訊協議,因此連續的http請求響應是獨立的,那麼如何讓http請求記住一些信息,好比登錄啊,購物車的商品啊,cookie就是其中的一個解決方案。它存在於瀏覽器中,咱們能夠寫代碼的時候經過request獲取存在本地的cookie。java
cookie的方法以下:web
(get/set)MaxAge:cookie是有生命週期的,咱們能夠設置它的生命週期,默認爲關閉瀏覽器則刪除。經過這個方法咱們就可讓用戶一個月免登錄。編程
(is/set)HttpOnly:咱們可讓cookie對js透明,若是將cookie的hhtponly設置爲true,則瀏覽器的腳本語言沒法獲取。數組
getName、getValue:獲得鍵值對的鍵和值。瀏覽器
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/cookie.view") public class CookieServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub resp.setContentType("text/html; charset=UTF-8"); PrintWriter out = resp.getWriter(); out.println("---- Cookie tests ----<br />"); Cookie[] cookies = req.getCookies(); //從reqesut裏獲得cookies if(cookies != null) { for(Cookie c : cookies) { out.print(c.getName() + ":" + c.getValue() + ", max age is " + c.getMaxAge() + "<br />"); out.println(); } } else { Cookie cookie = new Cookie("tmac", "basketball"); cookie.setMaxAge(24 * 60 *60); resp.addCookie(cookie); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub } }
如代碼所示,httprequest提供getcookies方法,這個方法返回的是cookie數組,這樣咱們就能讀取已經在瀏覽器存在的鍵值對了,httpresponse提供addcookie方法,咱們能夠經過這個方法,將須要存的數據存在瀏覽器。cookie
已經瞭解cookie的基本知識了,如今作個自動登錄的demo。session
IndexServlet,能夠把這個servlet當作主頁,若是沒登錄,跳轉到登錄頁面,登錄則顯示用戶信息。ide
package demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/index.do") public class IndexServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub Cookie [] cookies = req.getCookies(); if(cookies != null) { for(Cookie c : cookies) { String name = c.getName(); String value = c.getValue(); if(name.equals("user") && value.equals("123")) { req.getRequestDispatcher("/user.do").forward(req, resp); return ; } } } resp.sendRedirect("login.html"); } }
登錄頁面ui
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> login </title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form action="/login.do" method="get"> <input type="text" name="name"/> <br /> <input type="text" name="value"/> <br /> <input type="submit" /> </form> </body> </html>
LoginServlet ,用來處理登錄表單
package demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/login.do") public class LoginServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String name = req.getParameter("name"); String value = req.getParameter("value"); if(name.equals("user") && value.equals("123")) { Cookie c = new Cookie("user", "123"); resp.addCookie(c); req.getRequestDispatcher("/user.do").forward(req, resp); return ; } else { resp.sendRedirect("login.html"); } } }
UserServlet,用來顯示信息
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user.do") public class UserServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub Cookie [] cookies = req.getCookies(); if(cookies != null) { for(Cookie c : cookies) { String name = c.getName(); String value = c.getValue(); if(name.equals("user") && value.equals("123")) { resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.print("hello, " + name + ", your value is " + value); return ; } } } resp.sendRedirect("login.html"); } }