有沒有好心人幫我看看這個關(guān)于Cookie的問題
首選時index.jsp頁面代碼:
<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>
? <%
?? long i=System.currentTimeMillis() ;
?? Date d=new Date(i);
?? SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh時mm分ss秒");
?String t=sdf.format(d);
? %>
? <body>
??? <h1>通過post方式傳遞一個參數(shù)給detail.jsp頁面</h1><br>
?? ?<form action="detail.jsp" method="post">
?? ??<table>
?? ???<tr>
?? ????<td><input type="text"? name="time"? value=<%=t %>></td>
?? ????<td><input type="submit" value="提交當(dāng)前時間"></td>
?? ???</tr>
?? ??</table>
?? ?</form>
? </body>
</html>
其次是detail.jsp頁面代碼:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
???
??? <title>處理傳遞過來的參數(shù)</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>
???? <h1>接受傳遞過來的參數(shù)</h1>
???? <%
???? ?request.setCharacterEncoding("utf-8");
???? ?String time= request.getParameter("time");//接受傳遞過來的參數(shù)
????
???? %>
??? 傳遞過來的參數(shù)是:<%=time %><br>
??? <%
??? ?//將傳遞過來的參數(shù)保存到Cookie中
??? ?//直接創(chuàng)建Cookie對象,就會導(dǎo)致每一訪問的時候都會創(chuàng)建一個最新的Cookie對象,
??? ?//所以我們可以先判斷有沒有Cookie對象,并將要保存的值先保存到一個字符串變量中,
??? ?//然后再取出
??? ?Cookie[] cookies=request.getCookies();
??? ?if(cookies !=null && cookies.length>0){
??? ??for(Cookie c:cookies){
??? ??time +=?"<br>"+c.getValue()+"#";//將cookie對象的值保存到time變量中,并以“#”分割
??? ??}
??? ?}
??? ?Cookie cookie=new Cookie("timecookie",time);
???
??? ?response.addCookie(cookie);
???? %>
???? <a href="cookie.jsp">在同一次會話中獲取保存的cookie</a>
? </body>
</html>
然后是cookie.jsp頁面代碼:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <base href="<%=basePath%>">
???
??? <title>My JSP 'cookie.jsp' starting page</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>
??? <%
??? String creatCookieTime=null;
??? ?Cookie[] cookies=request.getCookies();
??? ?for(Cookie c:cookies){
??? ??if(c.getName().equals("timecookie")){
??? ???creatCookieTime=c.getValue();
??? ???out.println("獲得Cookie對象"+creatCookieTime);
??? ??}
??? ?}
??? ?String[] arr=creatCookieTime.split("#");
??? ?if(arr !=null && arr.length>0){
??? ??for(String s:arr){
??? ??
??? ?%>
??? <p>Cookie的創(chuàng)建時間是<%=s%><br></p>
??? <%
? ? ? } ? ?
??? ?}
???? %>?
? </body>
</html>
運行結(jié)果是:
2018-06-25
這里是想獲取ID嗎?
2018-03-22
發(fā)現(xiàn)你的一個錯誤