zoukankan      html  css  js  c++  java
  • Session 计数例子

    Servlet 文件

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class CountServlet
     */
    @WebServlet("/CountServlet")
    public class CountServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
         */
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out=response.getWriter();
            
            HttpSession session =request.getSession();  //获取Session对象
            System.out.println(session.getId());
            
            Integer count =(Integer)session.getAttribute("count"); //绑定数据
            
            if(count==null)
            {
                count=1;
            }
            else{
                count++;
            }
            
            session.setAttribute("count", count);   //获取绑定数据
            out.println("这个是"+count+"次访问");
            out.close();
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
    }
  • 相关阅读:
    IE6绿色版下载
    bcb 取相对路经
    MyEclipse 工具
    Flex与.NET互操作(一):基于Socket的网络连接
    兼容DC
    Flex与.NET互操作(二):基于WebService的数据访问(上)
    虚函数PostNcDestroy功能
    VC++多线程编程
    POJ 1222 extended lights out 高斯消元 板子题
    hihocoder 2015 北京区域赛 A Xiongnu's Land
  • 原文地址:https://www.cnblogs.com/lewenzhong/p/5462571.html
Copyright © 2011-2022 走看看