你的位置:首页 > Java教程

[Java教程]jsp—application 模拟计数器


 

 

 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 12 String str=application.getInitParameter("test2");13 out.print("test2="+str+"<br>");14 //模拟计数器15 //初始化16 //取出计数器的当前值17 Object obj=application.getAttribute("count");18 if(obj==null)19 {20   21 application.setAttribute("count", 1);22 obj=1;23 }24 25 //模拟计数器++(+1)的运行方式26 //数据转型,同object转成数值型27 long count=Long.parseLong(obj.toString());28 count++;29 //out.print("count="+obj.toString());30 out.print("count="+count);31 //取完之后将新的值在放到里面继续叠加运算32 application.setAttribute("count", count);33 %>34 </body>35 </html>