protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建对象
ServletContext sc = getServletContext();
//获取tongji的值,强转成Integer,可以接收null
Integer tongji=(Integer)sc.getAttribute("tongji");
//为null,说明是第一次访问
if(tongji==null){
tongji=1;
}else{
tongji++;
}
//把上面执行的结果,添加到ServletContext 中,完成一次记录
sc.setAttribute("tongji", tongji);
System.out.println("第"+sc.getAttribute("tongji")+"次访问");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = getServletContext();
Integer tongji=(Integer)sc.getAttribute("tongji");
//三元运算符:两个数等于,则是":"左边,不等于则是":"右边
//tongji==null则为0,不等于null则为tongji本身的数字
System.out.println("累积访问次数:"+(tongji==null?0:tongji));
}
ServletContext记录访问次数,三元运算符
猜你喜欢
转载自blog.csdn.net/qq_44063001/article/details/88131363
今日推荐
周排行