关于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对象是临时的,不要保存在硬盘中,关闭浏览器后数据会自动丢失!
相关文档:
jsp中文显示乱码解决方案2006-12-27 22:56
一、JSP页面显示乱码
二、表单提交中文时出现乱码
三、数据库连接
大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到
的中文乱码的问题及解决办法写出来供大家参考。
一、JSP页面显示乱码
下面的显示页面(display.jsp)就出现乱 ......
<%@ 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" ......
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="ccp.suddenattack.service.news.*"%>
......