web统计访问量

来源:互联网 发布:福州地铁软件 编辑:程序博客网 时间:2024/05/01 14:48

1.链接访问网页(jsp)
2.servlet(方法)

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("接收到");
ServletContext context = getServletContext();
int count = 0;
synchronized (context) {
if(null == context.getAttribute("counter")){
count = 1;
}else{
count = Integer.parseInt((String) context.getAttribute("counter"));
count = count+1;
}
context.setAttribute("counter", count+"");
req.setAttribute("counter1", count+"");
}
System.out.println("准备发送,已经访问"+count+"次");
req.getRequestDispatcher("/newCounter.jsp").forward(req, resp);
}

 3.配置web.xml

4.访问界面接受(jsp):

<%String conter = (String)pageContext.getServletContext().getAttribute("counter");
%>
<%String conter1 = (String)request.getAttribute("counter1"); %>
第三个网页
0 0