礼悟:
好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
explorer:Firefox
os:windows7 x64
ide:MyEclipse 2017
工程目录结构
web.xml
StatisticsTimesServletContext
jizuiku.web.servlet.StatisticsTimesServletContext
StatisticsTimesServletContext
/StatisticsTimesServletContext
代码
package jizuiku.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 在服务器关闭之前,统计该网页的浏览量。
*
* @author 给最苦
* @version V17.10.21
*/
public class StatisticsTimesServletContext extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext application = this.getServletContext();
// 有count返回值,没有count返回Null
Integer count = (Integer) application.getAttribute("count");
if (count == null) {
// 该网页第一次访问
count = 1;
} else {
// 非第一次访问
count++;
}
// 存储记录
application.setAttribute("count", count);
// 向浏览器页面输出内容
PrintWriter pw = response.getWriter();
pw.print("
" + count + "
");}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
效果
不刷新,再打开两个该页面
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。