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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章