jsp实现网站计数器

时间:2022-10-18 14:48:11
<%@ page contentType="text/html; charset=UTF-8" import="java.util.*" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML >
<html>
  <head>
    <title>我的第一个JSP页面</title>
  </head>
  <body>
    <%
       int number=0;
       synchronized(application)
       {
         if(application.getAttribute("times")==null)
         {
           number=1;
          }
          else
          {
            number=Integer.parseInt((String)application.getAttribute("times"));
            number++;
           }
           application.setAttribute("times", Integer.toString(number));
       }
     %>
     <table>
      <tr bgcolor="lightgrey">
      <td align="center">欢迎访问</td>
        </tr>
        <tr>
      <td align="center">你是第<b><%=number%></b>位访问本网站的用户!</td>
        </tr>
     </table>
  </body>
</html>