JSP+Servlet+JavaBean统计页面在线访问次数

时间:2021-06-26 19:09:00

统计页面浏览次数:使用的是servlet实现统计次数传递给JSP页面

说明:我做的都比较接地气,意思就是比较简单!

效果图如下:

JSP+Servlet+JavaBean统计页面在线访问次数

上代码

counter.java(它真的好简单,啥事不干,只是定义一个访问次数的基值):

1 package util;
2 
3 public class Counter {
4     public static int COUNT_ONLINE=1;
5 }

Count.java(servlet):它负责处理业务逻辑,获取访问次数并传递给JSP页面

特别注意:
ServletContext servletContext =this.getServletContext();
Object count = servletContext.getAttribute("count");
 1 package servlet;
 2 import java.io.IOException;
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import util.Counter;
 9 public class Count extends HttpServlet{
10     @Override
11     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
12             throws ServletException, IOException {
13         // 一般servlet容器启动时便会初始化一个ServleContext 他是被所有servlet共享的.,
14         //二ServletConfig是当前Servlet的"配置类:,但是ServletConfig中会维护这ServletContex
15         //这里它的作用相当于JSP页面的application
16         ServletContext servletContext =this.getServletContext();
17         Object count = servletContext.getAttribute("count");
18         if(count ==null){
19             servletContext.setAttribute("count", Counter.COUNT_ONLINE);
20         }else{
21             servletContext.setAttribute("count", Counter.COUNT_ONLINE++);
22         }
23         req.getRequestDispatcher("/count.jsp").forward(req, resp);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         doGet(req, resp);
31     }
32     @Override
33     protected void service(HttpServletRequest req, HttpServletResponse resp)
34             throws ServletException, IOException {
35         // TODO Auto-generated method stub
36         super.service(req, resp);
37     }
38     
39 }

web.xml

  <servlet>
      <servlet-name>count</servlet-name>
      <servlet-class>servlet.Count</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>count</servlet-name>
      <url-pattern>/servlet/count</url-pattern>
  </servlet-mapping>

count.jsp:很简单,就只是EL表达式获取下servlet传过来的count变量值

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11     Object message = session.getAttribute("sucess");
12     
13     if(message!=null && !"".equals(message)){
14 
15  %>
16      <script type="text/javascript">
17          alert("<%=message%>");
18      </script>
19  <%} %>
20  
21  <%
22  Object username = session.getAttribute("username");
23  if(username!=null && !"".equals(username)){
24  
25   %>
26   <%=username.toString() %>,欢迎你!<%} %>
27 hello word!
28 
29     
30     页面被访问:${count }
31 </body>
32 </html>