javaweb
**cookie 和 session**
1.cookie 和session的原理
1.1 解决HTTP协议无状态的问题
cookie 和session解决HTTP无状态采用的两种解决方案
(1)cookie:信息保存在浏览器
(2)session:信息保存在服务器
2.cookie 实现免登录
2.1 步骤:
(1)申请卡片
(2)指定卡片的使用规则
指定卡片的使用范围(服务器):
cookie.setPath(req.getServletContext()) 只能在当前项目中使用
cookie.setPath("/") 整个服务器均可使用
(3)把卡片交给用户
responce.addCookie(pwd)
注意:第一次,只有resp 有set-cookie
往后,request 里都带上Cookie
doServlet.java //1.申请卡片 Cookie n = new Cookie("n", uname); Cookie p = new Cookie("p", pwd); //2.设置卡片权限(当前项目) p.setPath(request.getContextPath()); n.setPath(request.getContextPath()); //2.1返回卡片 response.addCookie(n); response.addCookie(p);
(4)用户(浏览器使用)
request.getCookies()
<body> <% //获取服务器提供的cookie Cookie[] cookies = request.getCookies(); String name = ""; //cookie账号 String pwd = "";//cookie密码 for(Cookie cookie:cookies){ if("n".equals(cookie.getName())){ name = cookie.getValue(); } if("p".equals(cookie.getName())){ pwd = cookie.getValue(); } } %> <h3>欢迎来到login界面</h3> <form action="doServlet" method="post"> <%--填入对应的cookie--%> 用户名:<input type="text" onblur="isEmpty1()" name="uname" value="<%=name%>"><span id="alert1"></span> <br> 密码:<input type="password" onblur="_isEmpty()" name="pwd" value="<%=pwd%>"><span id="alert2"></span> <br> 记住密码<input type="checkbox"> <% Object obj = request.getAttribute("msg"); if (obj != null){ out.print(obj); } %> <br> <input type="submit"> </form> </body>
3.session的原理
4.session的失效时机
5.统计sxt官网访问人数

浙公网安备 33010602011771号