HttpSession实现计数

 

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		Object count=session.getAttribute("COUNT");
		int counter=0;
		if (count==null) {
			counter=1;
			session.setAttribute("COUNT",new Integer(1));
		}else{
			counter=((Integer)count).intValue();
			counter++;
			session.setAttribute("COUNT", new Integer(counter));
		}
		out.print("访问了"+counter+"次!");
	}

你可能感兴趣的:(servlet)