zoukankan      html  css  js  c++  java
  • Java Web servlet与cookie

    先来看看效果图:

     
    点击submit后:
    点击查看Cookies:
    在C:\Documents and Settings\Administrator\Cookies目录下面会有一个   hongten@webproject_cookie[2].txt  的文件;
    这就是cookie的文件;
     
     
    再来看看代码是怎样实现的:
    cookiesInput.html
    代码:
    <html>
    <head>
    <title>cookie input page</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body>
    请输入用户名(英文或者数字)<br>
    <form name="form1" action="SetCookiesServlet"  method="post" >
    <table border="0">
    <tr><td>用户名:</td>
    <td><input type="text" name="username">
    </td></tr>
    <tr><td colspan="2" align="center">
    <input name="submit" value="submit" type="submit">
    </td></tr>
    </table>
    </form>
    </body>
    </html>
     
     
     
    SetCookiesServlet.java
    代码:
    /**
     *
     */
    package webbook.chapter6;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import webbook.util.StringUtil;
    /**
     *
     * @author XHW
     *
     * @date 2011-7-22
     *
     */
    public class SetCookiesServlet extends HttpServlet {
     private static final long serialVersionUID = 6849802930688070121L;
     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      doPost(request, response);
     }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      String output = null;
      String username = request.getParameter("username");
      if (!StringUtil.validateNull(username)) {
       Cookie cookie1 = new Cookie("username", StringUtil
         .filterHtml(username));
       // cookie的有效期为1个月
       cookie1.setMaxAge(24 * 60 * 60 * 30);
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       Cookie cookie2 = new Cookie("lastTime", sdf.format(new Date()));
       cookie2.setMaxAge(24 * 60 * 60 * 30);
       response.addCookie(cookie1);
       response.addCookie(cookie2);
       output = "本次登录时间与用户名已经写到Cookie中。<br><a href=\"/webproject_cookie/GetCookiesServlet\">查看Cookies</a>";
      } else {
       output = "用户名为空,请重新输入。<br><a herf=\"/webproject_cookie/cookieInput.htm\">输入用户名</a>";
      }
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head><title>set cookies </title></head>");
      out.println("<body>");
      out.println("<h2>" + output + "</h2>");
      out.println("</body>");
      out.println("</html>");
      out.flush();
      out.close();
     }
    }
     
     
     
     
    GetCookiesServlet.java
    代码:
    /**
     *
     */
    package webbook.chapter6;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     *
     * @author XHW
     *
     * @date 2011-7-22
     *
     */
    public class GetCookiesServlet extends HttpServlet {
     private static final long serialVersionUID = -156960358689563625L;
     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      doPost(request, response);
     }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head><title>display login infomation</title></head>");
      out.println("<body>");
      out.println("<h2>从Cookie中获得上次登录时间与用户名</h2>");
    //获取所有的cookie值
      Cookie[] cookies = request.getCookies();
      Cookie cookie = null;
      for (int i = 0; i < cookies.length; i++) {
       cookie = cookies[i];
       if (cookie.getName().equals("username")) {
        out.println("用户名:" + cookie.getValue());
        out.println("<br>");
       }
       if (cookie.getName().equals("lastTime")) {
        out.println("上次登录时间:" + cookie.getValue());
        out.println("<br>");
       }
      }
      out.println("</body>");
      out.println("</html>");
      out.flush();
      out.close();
     }
    }
     
     
    cookie是web服务器通过浏览器保存在www用户端硬盘上的一个文本文件,这个文件中包含了文本信息
    文本信息的内容是以“名/值”对(key/value)的形式进行储存的
    可以让web开发者通过程序读写这个文本文件
    在Windows XP中保存Cookie的目录是:C:\Documents and Settings\用户名\Cookies
     
     
    Cookie的作用:
      解决浏览器用户与web服务器之间无状态通信
     
     
    使用cookie的时候注意的事项:
      cookie的大小和数量是有限制的
      cookie在个人硬盘上所保存的文本信息是以明文格式进行保存的,没有任何的加密措施
      浏览器可以设定不使用cookie 
     
  • 相关阅读:
    安卓监听帧动画结束
    零基础学python-13.4 文件上使用列表解析与列表解析扩展
    零基础学python-13.3 列表解析简介与步骤分解
    零基础学python-13.2 手动迭代:iter和next
    零基础学python-13.1 迭代器简介与文件迭代器
    零基础学python-12.6 使用for和zip来并行使用多个序列
    零基础学python-12.5 修改列表的误区以及使用for和range修改列表
    零基础学python-12.4 while、for与range联合使用
    零基础学python-12.3 for循环
    零基础学python-12.2 关键字pass,else,break,continue
  • 原文地址:https://www.cnblogs.com/hongten/p/2114448.html
Copyright © 2011-2022 走看看