【JSP实例】指定用户计数器

不同的用户访问次数是不一样的,因此对于每一个用户的访问次数都要进行统计,以适应需要。

用户登陆的Login.html的源文件:

<html>
<head>
<title>登录界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<BODY>
<CENTER>
<FONT SIZE=5 COLOR=BLUE>指定用户计数器</FONT>
</CENTER>
<BR>
<HR>
<BR>
<form method="post" action="userCounter.jsp" name="loginForm">
<CENTER>
请输入您的帐号:
<input type="text" name="textUserName">
<BR>
<input type="submit" name="Submit" value="确定">
<input type="reset" name="Reset" value="清除">
</CENTER>
</form>
</body>
</html>

计算访问次数的userCounter.JSP的源代码如下:

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="java.util.*;" %>
<%! 
    //使用HashTable计算访问次数的脚本代码:
    Hashtable userTable = new Hashtable(); 
        public int getCount( String userName )
        {
          
          Integer count = (Integer)userTable.get( userName );
          if( count != null )
          { 
            int nowCount = count.intValue() + 1;
            userTable.put( userName, new Integer( nowCount ) ); 
            return nowCount;
          }        
          userTable.put( userName, new Integer( 1 ) );
            return 1;
        }    
%>
<% 
    String username = request.getParameter( "textUserName" );
    int count = getCount( username );
%>
<html>
<head>
<title>指定用户计数器</title>
</head>
<body>
<CENTER>
<FONT SIZE=5 COLOR=BLUE>指定用户计数器</FONT>
</CENTER>
<BR>
<HR>
<BR>
<div align="center">您好!
<font color="#3333FF"><b><%= username %></b></font>
您已经到本站参观了
<font color="#3333FF"><b><%= count %></b></font></div>
</body>
</html>

效果图:

原文地址:https://www.cnblogs.com/4everlove/p/3392282.html