<%@page import="java.net.URLDecoder"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Coolie创建 用户登录
<% request.setCharacterEncoding("utf-8"); String userName = ""; String password = ""; Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(Cookie c:cookies){ if(c.getName().equals("userName")){ userName=URLDecoder.decode(c.getValue(), "utf-8"); } if(c.getName().equals("password")){ password=URLDecoder.decode(c.getValue(), "utf-8"); } } } %>
<%@page import="java.net.URLEncoder"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>登陆成功或失败 登陆成功
<% /* 首先判断用户是否选择了记住用户名 */ String[] isCookie=request.getParameterValues("isUseCookie"); if(isCookie!=null&&isCookie.length>0){ request.setCharacterEncoding("utf-8"); //把用户名和密码保存再cookie对象里 String userName=URLEncoder.encode(request.getParameter("userName"),"utf-8"); //使用URL解决无法在cookie中无法保存中文 String password=URLEncoder.encode(request.getParameter("password"),"utf-8"); Cookie cookieName=new Cookie("userName",userName); Cookie cookiePassword=new Cookie("password",password); cookieName.setMaxAge(864000); cookiePassword.setMaxAge(864000);//设置最大生存期限为10天 response.addCookie(cookieName); response.addCookie(cookiePassword); }else{ //把已经保存的Cookie内容失效 Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(Cookie c:cookies){ if(c.getName().equals("userName")||(c.getName().equals("password"))){ c.setMaxAge(0);//设置cookie失效 response.addCookie(c); } } } } %> 查看用户信息
<%@page import="java.net.URLDecoder"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>用户信息 用户信息
<% request.setCharacterEncoding("utf-8"); String userName = ""; String password = ""; Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie c : cookies) { if (c.getName().equals("userName")) { userName = URLDecoder.decode(c.getValue(), "utf-8"); } if (c.getName().equals("password")) { password = URLDecoder.decode(c.getValue(), "utf-8"); } } } %> 用户名:<%=userName%> 密码:<%=password%>