会话跟踪-Cookie机制-记录用户访问次数
来源:互联网 发布:java对象转json字符串 编辑:程序博客网 时间:2024/06/05 17:26
1.cookie是什么?
答:cookie其实是一小段的文本信息,客户端请求服务器,如果服务器需要记录客户端的状态,就使用response给客户端浏览器颁发一个cookie,客户端会把cookie保存起来。当浏览器再次请求时,会把地址和cookie一起提交给服务器,服务器检查该cookie,以此来辨认浏览器的状态,服务器还可以根据需要修改cookie的值。
2.获取浏览器端提交的所有cookie:Cookie[] cookies=request.getCookie();
3.向客户端设置cookie
response.addCookie(cookie1);
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="login.jsp"%> <!--1.注意errorPage--><!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><% request.setCharacterEncoding("UTF-8"); String username=""; int visitTimes=0; Cookie[] cookies=request.getCookies(); for(int i=0;cookies!=null&&i<cookies.length;i++){ Cookie cookie=cookies[i]; if("username".equals(cookie.getName())){ username=cookie.getValue(); } else if("visitTimes".equals(cookie.getName())){ visitTimes=Integer.parseInt(cookie.getValue()); cookie.setValue(++visitTimes+""); } } if(username==null||username.trim().equals("")){ throw new Exception("您还没有登录,请先登录。"); } Cookie visitTimesCookie=new Cookie("visitTimes",Integer.toString(visitTimes)); response.addCookie(visitTimesCookie); //修改cookie,更改访问次数,服务器端向客户端设置Cookie%><body> <form action="login.jsp" method="post"> <table> <tr> <td>您的账号</td> <td><%=username%></td> </tr> <tr> <td>登陆次数</td> <td><%=visitTimes %></td> </tr> <tr> <td><%=request.getRequestURI() %>-----URL:<%=request.getRequestURL() %></td> <!--URI:/servletDemo/cookie.jsp--> <!--URL:http://localhost:8080/servletDemo/cookie.jsp--> <td><input type="button" value="刷新" onclick="location='<%=request.getRequestURI()%>?ts='+new Date().getTime();" class="button"></td> </tr> </table> </form></body></html>
//登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %> <!--注意isErrorPage--><!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> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); if("POST".equals(request.getMethod())){ Cookie usernameCookie=new Cookie("username",request.getParameter("username")); Cookie visitTimesCookie=new Cookie("visitTimes","0"); response.addCookie(usernameCookie); response.addCookie(visitTimesCookie); response.sendRedirect(request.getContextPath()+"/cookie.jsp"); return; } %> <form action="login.jsp" method="post"> <table> <tr> <td>request.getContextPath():<%=request.getContextPath() %></td> <!--servletDemo--> <td><span style="color: red;"><%=exception.getMessage() %></span></td> </tr> <tr> <td>账号:</td> <td><input type="text" name="username" style="width:200px;"></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="password" style="width:200px; "></td> </tr> <tr> <td></td> <td><input type="submit" value="登录" class="button"></td> </tr> </table> </form></body></html>
阅读全文
0 0
- 会话跟踪-Cookie机制-记录用户访问次数
- 使用cookie记录用户访问次数
- cookie记录用户次数
- Cookie记录访问次数
- Java中使用Cookie记录用户访问次数
- Cookie实现会话跟踪!
- Cookie实现会话跟踪
- Cookie,Session会话跟踪
- Cookie会话跟踪技术
- 巧用JavaScript记录访问次数(Cookie)!
- Servlet中用Cookie记录访问次数实例:
- cookie本地信息记录访问次数
- 跟踪用户会话
- 浅谈用户会话跟踪
- 用servlet记录用户访问次数
- JSPServlet会话跟踪机制
- JSPServlet会话跟踪机制
- JSPServlet会话跟踪机制
- C#之入门总结_文件_2_13
- Random.Range
- mysql 5.6免安装版本的配置和安装
- 虚幻4开发小技巧
- IO流——File类列出文件列表list() listFiles()
- 会话跟踪-Cookie机制-记录用户访问次数
- String.IndexOf()
- 趣味编程-JavaScript-使用画布画画canvas
- xpath
- java创建数据库连接池代码
- 未来3个月短期目标(2017.8.8-2017.11.8)
- Linux下常用命令行及其参数整理
- SpringMVC 欢迎页 index.jsp 中 i18n国际化失效问题解决
- String.Substring()