什么是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
<% 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); %>