Cookie代码例子

    xiaoxiao2021-03-25  79

    什么是cookie?

    Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。用户每次访问站点时,Web 应用程序都可以读取 Cookie 包含的信息。

     

    记住密码的登录:

     

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

    Insert title here

    登录页面

    用户名: 密码:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here

    你好,欢迎来到index.jsp

    <% String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); if("admin".equals(uname) && "123".equals(upwd)){ Cookie cookieName=new Cookie("uname",uname); Cookie cookiePwd=new Cookie("uname",upwd); cookieName.setMaxAge(30); cookiePwd.setMaxAge(30); response.addCookie(cookieName); response.addCookie(cookiePwd); }else{ Cookie cookies[]=request.getCookies(); if(cookies!=null && cookies.length>0){ for(Cookie c:cookies){ if("admin".equals(uname)){ uname=c.getValue(); } if("123".equals(upwd)){ upwd=c.getValue(); } } } } if("admin".equals(uname) && "123".equals(upwd)){ out.print("欢迎"+uname); }else{ response.sendRedirect("login.jsp"); } %>  

     

    浏览历史:

     

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%>

    Insert title herea b c d e

     

    浏览历史 <% Cookie cookies[]=request.getCookies(); if(cookies!=null && cookies.length>0){ for(Cookie c:cookies){ out.println(""+c.getValue()+" "); } } %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%>Insert title here<%=request.getParameter("title") %> 返回 <% String title=request.getParameter("title"); Cookie cookie=new Cookie("title"+title,title); response.addCookie(cookie); %>

     

    转载请注明原文地址: https://ju.6miu.com/read-37881.html

    最新回复(0)