关于jsp中的cookie小程序
<body>
<%
Cookie[] cookies = request.getCookies() ;
Cookie cookie = null ;
if(cookies!=null){
cookie = cookies[0] ;
out.print("上次访问的ip是: " + request.getRemoteAddr() +"<br>") ; //得到客户端的ip
out.print("上次访问时间:" + cookie.getValue()) ;
cookie.setValue(new java.util.Date().toString()) ;
response.addCookie(cookie) ; //保存到客户端
cookie.setMaxAge(365 * 24 * 3600);
}
if(cookies==null){
cookie = new Cookie("AccessTime","") ;
cookie.setValue(new java.util.Date().toString()) ;
response.addCookie(cookie) ; //保存到客户端
cookie.setMaxAge(365 * 24 * 3600);
}
%>
</body>
ps:如果没有设置cookie的有效期,那么在关闭浏览器时cookie会被删除。如果生命期为负整数,表示这个cookie对象是临时的,不要保存在硬盘中,关闭浏览器后数据会自动丢失!
相关文档:
get
jsp url ${param.serialID}
action jsp String commentId = ServletActionContext.getRequest().getParameter("commentId");
-- ......
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> ......
<html:html>
<HEAD>
<tiles:insert attribute="header" />
</HEAD>
<body>
<tiles:insert attribute="top" />
<table width="100%" height="400" border="0" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td width="178" ......
<table width=100% border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="1" class="gray1"></td>
</tr>
</table>
<tiles:insert attribute="footer" />
</body>
</html:html> ......