简单分析,由于http协议的无状态性不能保存数据,我们利用cookie保存用户上次登陆该servlet的时间,第一次登陆告诉是第一次登陆还没记录,(servlet中获取到的cookie值为空说明是第一次登陆这时设置上cookie的值保存这次登陆的时间)第二次及以后则显示上次的登陆时间(servlet中获取到的cookie值不为空,将cookie中的值显示即上次登录的时间)。
代码:
package com.zc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestCookie2 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public TestCookie2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
/*获取上册登陆的时间*/
GregorianCalendar gc=new GregorianCalendar();
String time=gc.get(Calendar.YEAR)+"-"+gc.get(Calendar.MONTH)+"-"+gc.get(Calendar.DATE)+"-"+gc.get(Calendar.HOUR)+":"+gc.get(Calendar.MINUTE)+":"+gc.get(Calendar.SECOND);//存放系统获取的时间
if(gc.get(Calendar.AM_PM)==1){//判断上次登陆是上午还是下午
time+=" PM";
}else{
time+=" AM";
}
Cookie timeCookie = new Cookie("lastTime",time);//存放上次登录时间的cookie
timeCookie.setMaxAge(3600*24*365);//设置cookie的有效时间是一年
response.addCookie(timeCookie);//添加时间cookie
Cookie[] Cookies = request.getCookies();//获取所有cookie
String lastTime=null;//存放上次登陆的时间
for(int i=0;Cookies!=null&&i<Cookies.length;i++){
if("lastTime".equals(Cookies[i].getName())){
lastTime=Cookies[i].getValue();
break;
}
}
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(lastTime==null){
out.println("您是第一次登陆");
}else{
out.println("上次登陆的时间是:"+lastTime);
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
从上面的试验中可以看出cookie保存的总是上一次访问该网站时的信息。