需求
- 访问一个servlet,如果是第一次访问,则提示您好欢迎您首次访问
- 如果不是,则提示欢迎回来,显示上次访问的时间
分析:可以采用cookie来完成
在往服务器中servlet判断是否有一个名为last time的cookie
- 有:不是第一次访问
- 没有:是第一次访问
- 响应数据
- 写回cookie
public class Cookietest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置消息体响应的数据格式编码
response.setContentType("text/html;charset=utf-8");
boolean flag = false;
//获取所有的cookie
Cookie[] cc = request.getCookies();
if(cc!=null && cc.length>0){
for(Cookie c : cc){
if("lastTime".equals(c.getName())){
//获取cookie的value
// 设置cookie的值
String v = c.getValue();
v= URLDecoder.decode(v,"utf-8");//url解码
response.getWriter().write("欢迎欢迎回来"+v);
//获取当前时间的字符串
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url编码
c.setValue(st_date);
response.addCookie(c);
flag=true;
//设置cookie的存活时间
c.setMaxAge(60*60*24*30);
break;
}
}
}
if(cc == null || cc.length==0 || flag == false) {
//获取当前时间的字符串
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url编码
Cookie lastTime = new Cookie("lastTime", st_date);
response.addCookie(lastTime);
//设置cookie的存活时间
lastTime.setMaxAge(60*60*24*30);
st_date = URLDecoder.decode(st_date,"utf-8");
response.getWriter().write("<h1>欢迎首次访问" + st_date+"<h1>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
jsp样式
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//设置消息体响应的数据格式编码
response.setContentType("text/html;charset=utf-8");
boolean flag = false;
//获取所有的cookie
Cookie[] cc = request.getCookies();
if(cc!=null && cc.length>0){
for(Cookie c : cc){
if("lastTime".equals(c.getName())){
//获取cookie的value
String v = c.getValue();
v= URLDecoder.decode(v,"utf-8");//url解码
response.getWriter().write("欢迎欢迎回来"+v);
// 设置cookie的值
//获取当前时间的字符串
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url编码
c.setValue(st_date);
response.addCookie(c);
flag=true;
//设置cookie的存活时间
c.setMaxAge(60*60*24*30);
break;
}
}
}
if(cc == null || cc.length==0 || flag == false) {
//获取当前时间的字符串
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url编码
Cookie lastTime = new Cookie("lastTime", st_date);
response.addCookie(lastTime);
//设置cookie的存活时间
lastTime.setMaxAge(60*60*24*30);
st_date = URLDecoder.decode(st_date,"utf-8");
response.getWriter().write("<h1>欢迎首次访问" + st_date+"<h1>");
}
%>
</body>
</html>