实现功能:不同设备之间读取本地的cookie,互不影响。无cookie值或浏览器禁用cookie时有固定值
注意:从目前资料来看,用jsp操作cookie必须要用到两个页面。
1,保存cookie——saveCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'saveCookie.jsp' starting page</title>
</head>
<body>
<%
//获取上一页面表单提交过来的数据
String days = request.getParameter("days");
String refreshTime = request.getParameter("refreshTime");
String pagesize = request.getParameter("pagesize");
String deviation = request.getParameter("deviation");
//将值存入cookie
Cookie daysCookie = new Cookie("days",days );
Cookie refreshTimeCookie = new Cookie("refreshTime",refreshTime );
Cookie pagesizeCookie = new Cookie("pagesize",pagesize );
Cookie deviationCookie = new Cookie("deviation",deviation );
//设置存活期 默认值是-1,表示关闭浏览器,cookie就会消失。如果是正数,表示从现在开始,即将过期的seconds。为0则立即删除。
daysCookie.setMaxAge(60*60*24*365); //存活期为1年
refreshTimeCookie.setMaxAge(60*60*24*365);
pagesizeCookie.setMaxAge(60*60*24*365);
deviationCookie.setMaxAge(60*60*24*365);
//将cookie传递出去
response.addCookie(daysCookie);
response.addCookie(refreshTimeCookie);
response.addCookie(pagesizeCookie);
response.addCookie(deviationCookie);
%>
</body>
</html>
2,读取修改cookie——readCookie.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>修改參數</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/updata.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"></link>
<link href="css/site.css" rel="stylesheet"/>
<%
//读取cookie
Cookie cookies[]=request.getCookies();
//设置初值
String days ="1";
String refreshTime ="10";
String pagesize ="7";
String deviation ="10";
//获取对应名称的cookie的值
if(cookies!=null){
for(Cookie newCookie:cookies){
if(newCookie.getName().equals("days"))
{
days = newCookie.getValue();
}
if(newCookie.getName().equals("refreshTime"))
{
refreshTime = newCookie.getValue();
}
if(newCookie.getName().equals("pagesize"))
{
pagesize = newCookie.getValue();
}
if(newCookie.getName().equals("deviation"))
{
deviation = newCookie.getValue();
}
}
}
%>
</head>
<body>
<nav class="navbar navbar-default top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="board.jsp"><img src="images/logo.png" /></a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<span class="caption" style="font-size:30px;color:#081C7C">參數設置頁面</span>
</div>
</div>
</nav>
<form action="saveCookie.jsp" method="post" target="nm_iframe">
<div class="container-fluid" style="TEXT-ALIGN: center">
<div class="row type" style="margin-top:4%">
<div class="col-md-3"></div>
<div class=" col-md-3">
<div style="float:top">
<label class="lsize">查詢結束工單(天)</label>
</div>
<input id="days" name="days" οnclick=data-required type="number" max="60" min="1" class="lgInput form-control" value="<%= days%>"/>
</div>
<div class="col-md-3">
<div style="float:top">
<label class="lsize">翻頁間隔(秒)</label>
</div>
<input id="refreshTime" name="refreshTime" data-required type="number" max="200" min="1" class="lgInput form-control" value="<%= refreshTime%>"/>
</div>
<div class="col-md-3"></div>
</div>
<div class="row type" style="margin-top:2%">
<div class="col-md-3"></div>
<div class="col-md-3">
<div style="float:top">
<label class="lsize">顯示條數</label>
</div>
<input id="pagesize" name="pagesize" data-required type="number" max="100" min="1" class="lgInput form-control" value="<%= pagesize%>"/>
</div>
<div class=" col-md-3">
<div style="float:top">
<label class="lsize">收率的偏差率(%)</label>
</div>
<input id="deviation" name="deviation" οnclick=data-required type="number" max="100" min="0" class="lgInput form-control" value="<%= deviation%>"/>
</div>
<div class="col-md-3"></div>
</div>
<div class="row type" style="margin-top:2%">
<div class="col-md-4"></div>
<div class=" col-md-3" style="display:none">
<div style="float:top">
<label class="lsize">刷新時間(分)</label>
</div>
<input id="pageRefresh" name="pageRefresh" οnclick=data-required type="number" min="1" class="lgInput form-control" value="2"/>
</div>
<div class="col-md-4">
<input type="submit" name=sub value="确定" class="btn" οnclick="saveData()"/>
</div>
<div class="col-md-4"></div>
</div>
</div>
</form>
<!-- 提交,隐藏跳转-->
<iframe id="id_iframe" name="nm_iframe" style="display:none;"></iframe>
</body>
</html>
3,在另一页面获取cookie——getCookie.jsp
<%
Cookie cookies[]=request.getCookies();
int refreshTime = 10;
int pagesize = 7;
int deviation = 10;
if(cookies!=null){
for(Cookie newCookie:cookies){
if(newCookie.getName().equals("refreshTime"))
{
refreshTime = Integer.parseInt(newCookie.getValue());
}
if(newCookie.getName().equals("pagesize"))
{
pagesize = Integer.parseInt(newCookie.getValue());
}
if(newCookie.getName().equals("deviation"))
{
deviation = Integer.parseInt(newCookie.getValue());
}
}
}
%>
function saveData() {
var refreshTime = $("#refreshTime").val();
var pasesize = $("#pagesize").val();
var pageRefresh = $("#pageRefresh").val();
var days = $("#days").val();
var deviation = $("#deviation").val();
if (days == "" ||refreshTime == "" || pagesize == "" || pageRefresh == "" || deviation == "")
{
alert("配置項不能為空");
} else {
var v=document.getElementById("days").value;
if((/^[0-9]*[1-9][0-9]*$/.test(v) )&& v>0)
{
}else{
alert("查詢天數需為正整數,請重輸");
return false;
}
if(v<0|| v>60)
{
alert("查詢天數範圍為1-60,請重輸");
return false;
}
var v=document.getElementById("refreshTime").value;
if((/^[0-9]*[1-9][0-9]*$/.test(v) )&& v>0)
{
}else{
alert("翻頁間隔需為正整數,請重輸");
return false;
}
if(v<1 || v>200)
{
alert("翻頁間隔範圍為1-200,請重輸");
return false;
}
var v=document.getElementById("pagesize").value;
if((/^[0-9]*[1-9][0-9]*$/.test(v) )&& v>0)
{
}else{
alert("顯示條數需為正整數,請重輸");
return false;
}
if(v<1 || v>100)
{
alert("顯示條數範圍為1-100,請重輸");
return false;
}
var v=document.getElementById("pageRefresh").value;
if((/^[0-9]*[1-9][0-9]*$/.test(v) )&& v>0)
{
}else{
alert("刷新時間需為正整數,請重輸");
return false;
}
if(v<1 || v>60)
{
alert("刷新時間範圍為1-60,請重輸");
return false;
}
var v=document.getElementById("deviation").value;
if(/^\d+$/.test(v) )
{
}else{
alert("偏差率需為整數,請重輸");
return false;
}
if(v<0|| v>100)
{
alert("偏差率範圍為0-100請重輸");
return false;
}
$.ajax({
type : "post",
url : "board.jsp",
success : function(data)
{
alert("保存成功");
window.location.href = "board.jsp";
}
});
}
}
简洁版:
1,保存cookie
<%
String userName=request.getParameter("username");
Cookie cookie=new Cookie("username",userName);
cookie.setMaxAge(60*60*24*365);
response.addCookie(cookie);
%>
2,读取cookie
<span style="font-size:18px;"><%
Cookie cookies[]=request.getCookies();
String days=" ";
if(cookies!=null){
for(Cookie newCookie:cookies){
if(newCookie.getName().equals("days"))
{
days = newCookie.getValue();
System.out.println(days);
%> </span>
参考资料
Cookie的生命周期问题 - “阿米达”的父亲 - 博客园:http://www.cnblogs.com/jirglt/archive/2012/10/20/2732255.html
java web中cookies的用法 转 - carbs - 博客园: http://www.cnblogs.com/carbs/archive/2013/01/22/2870804.html
java中Cookie的使用 - haozhongjun的专栏 - 博客频道 - CSDN.NET: http://blog.csdn.net/haozhongjun/article/details/17239721