JSP页面自增

ffffffffffffffffffffffffffffffff

<c:set value="0" var="seq" /> <!-- 序号 -->  
<c:set value="${seq + 1}" var="seq" />  <!-- 序号自增 -->
<td>${seq} </td>

使用JSP实现网站访问量计数器页面有多种方法,以下为几种常见方式: ### 使用JSP和Java Bean结合文本文件 采用JSP和Java Bean结合文本文件的方式,用到两个文件,`test.jsp`文件用于在浏览器中运行,`counter.java`是后台的一个小Java Bean程序,用来读计数器的值和写入计数器的值。对于计数器的保存,采用一个文本文件`lyfcount.txt` [^1]。 ### 使用application对象 使用`application`对象实现网站计数器,示例代码如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%! int i = 0 ; synchronized void count(){ i++; } %> <% count(); %> 您是第<%=i %>个访问该页面的人 </body> </html> ``` 此代码定义了一个全局变量`i`,并使用同步方法`count`对其进行自操作,每次访问页面时`i`的值会加1 [^2]。 ### 防刷新计数器 通过`session`和`application`对象实现防刷新的网站访问量计数器,代码如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>网站访问量计数器</title> </head> <body> <% if(session.getAttribute("visit")==null){ session.setAttribute("visit", "y");//将未访问设置为访问 String strCount = (String) application.getAttribute("count"); int count =0; if(strCount!=null) count = Integer.parseInt(strCount)+1; else count++; application.setAttribute("count",String.valueOf(count)); %> <%}%> 您是第<%=application.getAttribute("count") %>位访问者! </body> </html> ``` 该代码通过判断`session`中是否有`visit`属性来确定是否为新的访问,如果是新访问则更新`application`中的计数器值 [^3]。 ### 使用JSP标签和Java Bean 使用JSP标签和Java Bean实现简单页面计数器,示例代码如下: ```jsp <%@ page contentType="text/html; charset=GBK" %> <jsp:useBean id="sessionCounter" scope="application" class="com.count.SessionCounter"/> <jsp:setProperty name="sessionCounter" property="*" /> 当前在线:<jsp:getProperty name="sessionCounter" property="currentSessionCount"/><br> 总访问量:<jsp:getProperty name="sessionCounter" property="totalSessionCount"/><br> ``` 此代码使用`<jsp:useBean>`标签引入一个Java Bean,并通过`<jsp:getProperty>`标签获取Bean中的属性值来显示当前在线人数和总访问量 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值