Session使用(14)

用session统计某个网页的访问人数(由于我还不会js,因此就作了个简易版本的)html

一、建立Session监听器,每建立了一个Session对象就执行监听类中·的sessionCreated方法。java

package com.ykh.session;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SeesionLister implements HttpSessionListener{
	 @Override
	    public void sessionCreated(HttpSessionEvent event) {
	        // System.out.println("建立了");
	        HttpSession session = event.getSession();// 得到Session对象
	        // 经过Session得到servletcontext对象
	        ServletContext servletContext = session.getServletContext();
	        /**
	         * 1.获取num值
	         * 2.加1
	         * 3.存入servletcontext
	         */
	        Object object = servletContext.getAttribute("num");
	        if (object == null) {
	            servletContext.setAttribute("num", 1);
	        } else {
	            Object num = servletContext.getAttribute("num");
	            int num1 = (int) num;
	            servletContext.setAttribute("num", num1 + 1);
	        }
	    }
	 
	    @Override
	    public void sessionDestroyed(HttpSessionEvent event) {
	        // System.out.println("销毁了");
	        HttpSession session = event.getSession();// 得到Session对象
	        // 经过Session得到servletcontext对象
	        ServletContext servletContext = session.getServletContext();
	        /**
	         *  1.获取num值
	         *  2.减1
	         *  3.存入servletcontext
	         */
	        servletContext.setAttribute("num", (long) servletContext.getAttribute("num") - 1);
	    }
}

  二、具体实现的servlet:session

package com.ykh.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
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 SeesionTest
 */
@WebServlet("/Test")
public class SeesionTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeesionTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
       doPost(req,resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();// 建立Session
        // session.setMaxInactiveInterval(10);//设置失效时间10秒 不是销毁
        PrintWriter writer = resp.getWriter();
        ServletContext servletContext = getServletContext();
        Object num = servletContext.getAttribute("num");
        writer.write("在线人数:" + num);
	}

}

  三、效果:ide

连接:https://pan.baidu.com/s/1F4R-jdC2bhKO7iTmzaOyWA
提取码:whkm
复制这段内容后打开百度网盘手机App,操做更方便哦3d

相关文章
相关标签/搜索