会话跟踪-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>