### Cookie应用测试
用Cookie**获得上次访问时间**
```java
public class Cookie_01 extends HttpServlet {
@Override
//保存用户上一次访问的时间
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
Cookie[] cookies = req.getCookies();
if(cookies !=null)
{
writer.print("你上一次访问的时间是");
for (int i = 0; i < cookies.length; i++) {
Cookie C= cookies[i];
//获取名字
if(C.getName().equals("TIME"))
{
long l = Long.parseLong(C.getValue());
Date date = new Date(l);
writer.print(date.toLocaleString());
}
}
} else
{
writer.print("第一次访问");
}
//服务器给客户端相应cookie
Cookie cookie = new Cookie("TIME",System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}