标签:删除 writer getname 解决 key col conf tty new
1 public class CookieDemo01 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 req.setCharacterEncoding("utf-8"); 6 resp.setCharacterEncoding("utf-8"); 7 resp.setContentType("text/html; charset=utf-8"); 8 9 PrintWriter out = resp.getWriter(); 10 11 //服务器从客户端获取cookie 12 Cookie[] cookies = req.getCookies(); 13 14 if (cookies != null) { 15 out.write("你上一次访问的时间是:"); 16 17 for (int i = 0; i < cookies.length; i++) { 18 Cookie cookie = cookies[i]; 19 20 //判断cookie的名字 21 if (cookie.getName().equals("lastLoginTime")) { 22 //获取cookie的值 23 long lastLoginTime = Long.parseLong(cookie.getValue()); 24 25 Date date = new Date(lastLoginTime); 26 27 out.write(date.toLocaleString()); 28 } 29 } 30 } else { 31 out.write("这是您第一次访问"); 32 } 33 34 //服务器给客户端响应一个cookie 35 Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + ""); 36 37 //设置cookie的有效期为1天 38 cookie.setMaxAge(24 * 60 * 60); 39 resp.addCookie(cookie); 40 } 41 42 @Override 43 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 44 doGet(req, resp); 45 } 46 }
1 Cookie[] cookies = req.getCookies(); //获取cookie 2 cookie.getName(); // 获取cookie的key 3 cookie.getValue(); //获取cookie的value 4 new Cookie("lastLoginTime", System.currentTimeMillis() + "");//新建一个cookie 5 cookie.setMaxAge(24 * 60 * 60); //设置cookie的有效期 6 resp.addCookie(cookie); //浏览器返回给客户端一个cookie
1 //服务器给客户端响应一个cookie 2 Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + ""); 3 4 //设置cookie的有效期为1天 5 cookie.setMaxAge(24 * 60 * 60); 6 resp.addCookie(cookie);
1 cookie = new Cookie("name",URLEncoder.encode("你好!","utf-8")) 2 out.write(URLDecoder.decode(cookie.getValue(),"utf-8"));
1 public class SessionDemo01 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 //解决中文乱码问题 6 req.setCharacterEncoding("utf-8"); 7 resp.setCharacterEncoding("utf-8"); 8 resp.setContentType("text/html; charset=utf-8"); 9 10 11 //得到Session 12 HttpSession session = req.getSession(); 13 14 15 //设置Session 16 session.setAttribute("name",new Person("小王",24)); 17 18 //获取Session的ID 19 String sessionId = session.getId(); 20 21 //判断session是不是新建的 22 if (session.isNew()){ 23 resp.getWriter().write("session创建成功,ID:"+sessionId); 24 }else{ 25 resp.getWriter().write("session已经在服务器中存在,ID:"+sessionId); 26 } 27 28 29 30 } 31 32 @Override 33 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 34 doGet(req, resp); 35 } 36 }
1 public class SessionDemo02 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 6 //解决中文乱码问题 7 req.setCharacterEncoding("utf-8"); 8 resp.setCharacterEncoding("utf-8"); 9 resp.setContentType("text/html; charset=utf-8"); 10 11 //得到Session 12 HttpSession session = req.getSession(); 13 14 Person person = (Person) session.getAttribute("name"); 15 16 resp.getWriter().write("获得person:" + person); 17 } 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 doGet(req, resp); 22 } 23 }
1 public class SessionDemo03 extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 HttpSession session = req.getSession(); 6 7 session.removeAttribute("name"); 8 9 //手动注销Session:注销之后就没有之前的那个Session但是会立即创建一个新的Session 10 session.invalidate(); 11 } 12 13 @Override 14 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 15 doGet(req, resp); 16 } 17 }
1 <!--设置15分钟后Session自动失效--> 2 <session-config> 3 <session-timeout>15</session-timeout> 4 </session-config>
标签:删除 writer getname 解决 key col conf tty new
原文地址:https://www.cnblogs.com/zhihaospace/p/12310376.html