<%
Integer count=null;
synchronized(application)
{
count=(Integer)application.getAttribute("basic");
if(count==null)
count=new Integer(0);
count=new Integer(count.intValue()+1);
application.setAttribute("basic",count);
}
%>
<%=count%>
Integer count=null;
synchronized(application)
{
count=(Integer)application.getAttribute("basic");
if(count==null)
count=new Integer(0);
count=new Integer(count.intValue()+1);
application.setAttribute("basic",count);
}
%>
<%=count%>
解决方案 »
- 急:struts2 <s:doubleselect> 无法生成二级菜单事件
- 模式窗口关闭时提示消息框如何弹出来
- 用ANT打包jar失败!
- eclipse tomcat 问题
- JSF报这个错,我该怎么处理?
- 如何得到一条记录中有几个字段?以及如何知道一个表有几个字段?
- Bean 类的构造函数是否一定要没有参数的?
- 怎么样使table中的每条记录在鼠标over时变颜色??????????????急
- 一个关于JavaBean的问题求教各位大侠,问题能解决200分相送!在线等待
- 有那位大侠知道firehose的,或者听说过!!!!
- 请教关于struts中的<logic:redirect>标签
- 如何从JSP文件向Javascript函数传递数组对象?
完全同意,强!顺便景仰你一下.
因为我没有想到synchronized的问题.自卑一下.......哎.
太客气了,有时间大家多交流!!
<!--jsp计数器-->
<%@ page import="java.io.*" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>
计数器演示程序
</TITLE>
</HEAD>
<BODY>
<%
//out.println(request.getHeader("Cookie"));
String currentRecord = null;//保存文本的变量
BufferedReader file; //BufferedReader对象,用于读取文件数据
//File f1=new File("count.txt");
// if(!f1.exists())f1.createNewFile();File nameOfTextFile = new File(application.getRealPath("counter.txt"));
if(!nameOfTextFile.exists()) nameOfTextFile.createNewFile();
//读取
file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr =null;
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入
try
{
readStr = file.readLine();
}
catch (IOException e)
{
System.out.println("读取数据错误.");
} if (readStr == null) readStr = "没有任何记录"; //判断cookie,第一次登陆时加1,刷新时不累计计数
else if (request.getHeader("Cookie")==null)
{
writeStr = Integer.parseInt(readStr)+1;
}
else
{
writeStr = Integer.parseInt(readStr);
} //写入时控制因为刷新引起的重复计数
if (request.getHeader("Cookie")==null)
{
try {
PrintWriter pw = new PrintWriter(new FileWriter(nameOfTextFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e) {
out.println(e.getMessage());
}
}
String count = String.valueOf(writeStr);
out.println(count);
%>
</BODY>
</HTML>