protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String lasttime = null;
String count = null;
Cookie c[] = request.getCookies();
for (int i = 0;c!= null && i<c.length;i++ ) {
if("last".equals(c[i].getName())){
lasttime = c[i].getValue();
break;
}
}
if(lasttime == null){
response.getWriter().write("<b>欢迎尼首次访问</b> <br>");
}else{
response.getWriter().write("<b>你上次访问的时间为: </b>"+"<b> "+lasttime+"</b> <br>");
}
String t = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date());
Cookie b = new Cookie("last",t);
response.addCookie(b);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
首次访问的时候,我发现程序会执行到for语句中的if判断中来,难道Cookie c[]一开始 不是为null和长度为0吗?还有就是第一次请求的时候是不是就会自动创建两个Cookie对象存在c中?(求大佬们详细解答!)