浏览器打开显示
listener
package cn.swpu.pang;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class OnlineUserCountListener implements HttpSessionListener {
public static int num=0;
public OnlineUserCountListener() {
}
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session创建");
num++;
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session销毁");
num--;
}
}
servlet
package cn.swpu.pang;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class IndexServlet
*/
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public IndexServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession(true);
request.getRequestDispatcher("/MyFisrtPage.jsp").forward(request, response);
}
}