JSP+Servlet+JavaBean統計頁面在線訪問次數


統計頁面瀏覽次數:使用的是servlet實現統計次數傳遞給JSP頁面

說明:我做的都比較接地氣,意思就是比較簡單!

效果圖如下:

上代碼

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>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM