HttpSession:会话跟踪

public void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException

{

  response.setContentType("text/html"); 

 //创建一个HttpSession   HttpSession session=request.getSession();

//回话的使用:session.setAbttribute(String name,Object value);保存一个对象

//session.getAbttribute(String name);取出保存对象,注意:返回类型是Object

 Object counter=session.getAttribute("COUNT");

 int count=0;

if(counter==null){

 count=1;

 session.setAttribute("COUNT",new Integer(1));

}else{

//intValue()将Integer类型转换成int类型

 count=((Integer)counter).intValue();

 count++;

 session.setAttribute("COUNT", new Integer(count));

   }

PrintWriter out=response.getWriter();

   out.print("欢迎你第"+count+"次访问");

   out.flush();

   out.close();

  }

posted @ 2011-11-14 21:56  张书薏  阅读(363)  评论(0编辑  收藏  举报