Servlet端写网站访问量:
JSP页面写显示访问量
用多种浏览器验证 只要服务器不重启
package com.mison;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestCounter extends HttpServlet {
public void init(javax.servlet.ServletConfig config)
throws ServletException {
System.out.println("访问器初始化开始。。。。。");
};
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext servletcontext = request.getSession()
.getServletContext();
if (null == servletcontext.getAttribute("Counter")) {
servletcontext.setAttribute("Counter", 1);
} else {
int counter = (Integer) servletcontext.getAttribute("Counter");
servletcontext.setAttribute("Counter", counter + 1);
}
RequestDispatcher pd= request.getRequestDispatcher("TestCounter.jsp");
pd.forward(request, response);
}
}
JSP页面写显示访问量
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TestCounter.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
已有<%=application.getAttribute("Counter") %>访问过此页面
</body>
</html>
用多种浏览器验证 只要服务器不重启