<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”ISO-8859-1″>
<title>主不在乎</title>
</head>
<body>
<%
String name=(String)session.getAttribute(“username”);
//获取cookie
Cookie[] cookies=request.getCookies();
//cookie存在
if(cookies !=null && cookies.length > 0){
for(Cookie cookie:cookies){
//获取cookie的名字
String cookieName=cookie.getName();
//判断是否与name相等
if(cookieName.equals(“name”)){
//获取cookie的值
String value=cookie.getValue();
name=value;
}
}
out.println(“welcome again: ” + name+”欢迎登录”);
//*************************
// 另一种写法
String v=null;
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals(“name”)){
v=cookies[i].getValue();
}
}
if(v!=null){
out.println(” Hello World “+v);
}
}
//*************************
else {
response.sendRedirect(“login.jsp”);
}
%>
<a href=”https://www.jb51.net/article/login.jsp” >relogin</a>
</body>
</html>