web統計訪問量

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"); %>
第三個網頁
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章