前两天我已经就这个问题请教过大家,有不少高手也指点了我,我也给了分。只是还有一个问题:各位高手的说法我看不懂,也不知道如何实现!:(
因为我是第一次做JSP程序,而且从来没有看过相关书籍,所以无知之处,请多多理解,只是问一个大家:象我这样的程序,偶尔会出现自动清0的情况,如何解决?在ASP中很简单,只要Application.lock和unlock就行了,或者可以用数据库实现;但现在我是改别人的程序,企图插入到现有程序中,目前不知道如何操作数据库;更是第一次做JSP程序,连环境都不知道如何配,现在只是用了NetBeans5.0勉强可以调试,所以请高手们麻烦一下说的清楚一些,或者给一点示范的源代码(相关技术实现的即可以)。最好用纯JSP实现,因为这样我可以插入别人的页面,别的办法不能保证一定能插入页面。
感激流鼻涕中......本人源码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%
String readStr = null; String temp = request.getParameter("entryID");
int itemp=0;
try{itemp = Integer.parseInt(temp);}catch (Exception e) {itemp = 0;} //转换传入的参数值,出错则使用非0值; String FileName = "Count" + itemp + ".txt"; String path = request.getRealPath("/LKCount");
File f = new File(path,FileName);
String nameOfTextFile = path + "\\" + FileName;
PrintWriter pw;
if(f.exists()==false){//检查Count****.txt是否存在,不存在则创建一个
try
{
f.createNewFile();//在当前目录下建立一个名为Count****.txt的文件,并写0至该文件;
pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println("0");
pw.close();
}
catch(IOException e){}
}
//============================判断文件是否存在,如不存在,则新建一个文件。 BufferedReader tempfile; //BufferedReader对象,用于读取文件数据
tempfile = new BufferedReader(new FileReader(nameOfTextFile)); readStr = tempfile.readLine(); //读取文件中的计数值
tempfile.close(); int writeStr=0;
if (readStr == null)
{
readStr = "没有任何记录";
}else{
writeStr = Integer.parseInt(readStr)+1;
}
//=======================================================读取文件,判断是否为空 if (f.exists())
{
try
{
pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e){ }
}%><%= writeStr %>
因为我是第一次做JSP程序,而且从来没有看过相关书籍,所以无知之处,请多多理解,只是问一个大家:象我这样的程序,偶尔会出现自动清0的情况,如何解决?在ASP中很简单,只要Application.lock和unlock就行了,或者可以用数据库实现;但现在我是改别人的程序,企图插入到现有程序中,目前不知道如何操作数据库;更是第一次做JSP程序,连环境都不知道如何配,现在只是用了NetBeans5.0勉强可以调试,所以请高手们麻烦一下说的清楚一些,或者给一点示范的源代码(相关技术实现的即可以)。最好用纯JSP实现,因为这样我可以插入别人的页面,别的办法不能保证一定能插入页面。
感激流鼻涕中......本人源码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%
String readStr = null; String temp = request.getParameter("entryID");
int itemp=0;
try{itemp = Integer.parseInt(temp);}catch (Exception e) {itemp = 0;} //转换传入的参数值,出错则使用非0值; String FileName = "Count" + itemp + ".txt"; String path = request.getRealPath("/LKCount");
File f = new File(path,FileName);
String nameOfTextFile = path + "\\" + FileName;
PrintWriter pw;
if(f.exists()==false){//检查Count****.txt是否存在,不存在则创建一个
try
{
f.createNewFile();//在当前目录下建立一个名为Count****.txt的文件,并写0至该文件;
pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println("0");
pw.close();
}
catch(IOException e){}
}
//============================判断文件是否存在,如不存在,则新建一个文件。 BufferedReader tempfile; //BufferedReader对象,用于读取文件数据
tempfile = new BufferedReader(new FileReader(nameOfTextFile)); readStr = tempfile.readLine(); //读取文件中的计数值
tempfile.close(); int writeStr=0;
if (readStr == null)
{
readStr = "没有任何记录";
}else{
writeStr = Integer.parseInt(readStr)+1;
}
//=======================================================读取文件,判断是否为空 if (f.exists())
{
try
{
pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e){ }
}%><%= writeStr %>
<%! private static Object lock = new Object();%>然后把你的代码用
<%
synchronized(lock){ ..... 你的所有的其他代码
}
%>这个方法是同步的,性能不好,但能用!