cookie记录用户次数

cookie.jsp页面

       <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="login.jsp"%>

<%

    request.setCharacterEncoding("UTF-8");

    String username = "";

    int visitTimes = 0;

   

    Cookie[] cookies = request.getCookies();//获取所有Cookie

    for(int i=0;cookies!=null&&i<cookies.length;i++){

         Cookie cookie = cookies[i];

         if("username".equals(cookie.getName())){

             username = cookie.getValue();

         }elseif("visitTimes".equals(cookie.getName())){

             visitTimes = Integer.parseInt(cookie.getValue());

         }

    }

    if(username==null||username.trim().equals("")){

         thrownew Exception("您还没有登录,请先登录");

    }

    //修改Cookie,更新用户的访问次数

    Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));

    //覆盖名为visitTimesCookie

    response.addCookie(visitTimesCookie);

     %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

  <body>

    <div align="center" style="margin:10px;">

    <fieldset>

         <legend>登录信息</legend>

         <form action="login.jsp" method="post">

             <table>

                 <tr><td>您的帐号:</td><td><%=username %></td></tr>

                 <tr><td>登录次数:</td><td><%=visitTimes %></td></tr>

<tr><td></td><td><input type="button" value="刷新" onclick="location='<%=request.getRequestURI() %>

?ts='+new Date().getTime();"/></td></tr>

             </table>  

         </form>

    </fieldset>

    </div>

  </body>

</html>

 

       login.jsp页面

       <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>

<%

    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 ;

    }

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

  <body>

     <div align="center" style="margin:10px;">

    <fieldset>

         <legend>登录</legend>

         <form action="login.jsp" method="post">

             <table>

                 <tr><td></td><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="password" name="pwd" style="width:200px;"/></td></tr>

                 <tr><td></td><td  align="center"><input type="submit" value="登录" style="width:100px;"/></td></tr>

             </table>

         </form>

    </fieldset>

    </div>

  </body>

</html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 cookie 记录用户访问同一网页的次数。当用户第一次访问网页时,可以在服务器端生成一个唯一的标识符,并将其存储在 cookie 中返回给客户端。每次客户端再次请求该网页时,可以将这个标识符作为参数传递给服务器端,服务器端可以从 cookie 中读取该标识符,并记录用户访问次数。 具体实现可以参考以下步骤: 1. 当用户第一次访问网页时,在服务器端生成一个唯一的标识符,并将其存储在 cookie 中返回给客户端。 ```python import uuid from flask import Flask, make_response, request app = Flask(__name__) @app.route('/') def index(): # 从请求中读取 cookie cookie = request.cookies.get('my_cookie') if cookie: # 如果 cookie 存在,则表示用户已经访问过该网页,将访问次数加 1 count = int(cookie) + 1 else: # 如果 cookie 不存在,则表示用户第一次访问该网页,将访问次数设为 1 count = 1 # 生成一个唯一的标识符,并将其存储在 cookie 中返回给客户端 resp = make_response('访问次数:%d' % count) resp.set_cookie('my_cookie', str(count)) return resp if __name__ == '__main__': app.run() ``` 2. 每次客户端再次请求该网页时,将该标识符作为参数传递给服务器端,并从 cookie 中读取该标识符。如果 cookie 存在,则表示用户已经访问过该网页,将访问次数加 1。如果 cookie 不存在,则表示用户第一次访问该网页,将访问次数设为 1,并将唯一的标识符存储在 cookie 中返回给客户端。 ```python import uuid from flask import Flask, make_response, request app = Flask(__name__) @app.route('/') def index(): # 从请求中读取 cookie cookie = request.cookies.get('my_cookie') if cookie: # 如果 cookie 存在,则表示用户已经访问过该网页,将访问次数加 1 count = int(cookie) + 1 else: # 如果 cookie 不存在,则表示用户第一次访问该网页,将访问次数设为 1 count = 1 # 生成一个唯一的标识符,并将其存储在 cookie 中返回给客户端 resp = make_response('访问次数:%d' % count) resp.set_cookie('my_cookie', str(count)) return resp if __name__ == '__main__': app.run() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值