<%@page import="java.net.URLEncoder"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <% request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String username="",password=""; //cookie //首先判断用户是否选择了登陆状态 String[] isUserCookies=request.getParameterValues("isUseCookie"); if(isUserCookies!=null&&isUserCookies.length>0) { //吧用户名和密码保存在cookie对象里面 username=URLEncoder.encode(request.getParameter("username"), "UTF-8"); password=URLEncoder.encode(request.getParameter("password"),"UTF-8"); Cookie usernameCookie=new Cookie("username",username); Cookie passwordCookie=new Cookie("password",password); usernameCookie.setMaxAge(864000);//设置最大生存期限为10天 passwordCookie.setMaxAge(864000); response.addCookie(usernameCookie); response.addCookie(passwordCookie); } else { 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); } } } } %> <a href="users.jsp">查看用户状态</a></body></html>
添加回答
举报
0/150
提交
取消