日期:2014-05-20 浏览次数:21042 次
Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { System.out.println(cookies[i].getName() + "=" + cookies[i].getValue()); }
------解决方案--------------------
1.这个一个页面模拟的cookie,如果你要实现登录,当用户输入用户名密码时,到控制层用
Cookie user = new Cookie("user",name+"-"+passward);
加到cookie中,然后在访问此登录页面时,就先调用
Cookie[] cookies=request.getCookies(); 看cookie中是否有user这个值,如果有就取得用户名、密码。
代码示例:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String name="nihao";
String passward="nihao";
try{
Cookie user = new Cookie("user",name+"-"+passward);
user.setMaxAge(60);
response.addCookie(user);
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
name=cookies[i].getValue().split("-")[0];
passward=cookies[i].getValue().split("-")[1];
request.setAttribute("name",name);
request.setAttribute("pass",passward);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="test10.jsp" method="post">
<input type="text" value="${name}" name=name /></br>
<input type="text" value="${pass}" name=pass />
<input type="submit" value="提交">
</form>
</body>
</html>