原文地址: http://www.java2000.net/viewthread.jsp?tid=7256
?我們先看一個(gè)使用文件的代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
?pageEncoding="UTF-8"%>
<%
? // 此文件的版權(quán)歸
? // Java世紀(jì)網(wǎng)(http://www.java2000.net)和 CSDN(www.csdn.net)所有
? // 作者:老紫竹
? // 轉(zhuǎn)載必須保留此版權(quán)聲明
%>
<%@page import="java.io.*"%>
<!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>Insert title here</title>
</head>
<body>
<%!// 一個(gè)同步用的鎖
? private static final Object lock = new Object();
? // 注意默認(rèn)的文件目錄在tomcat/bin目錄下面
? private static final File file = new File("counter.dat");%>
Hello Word!
<%
? Integer count = 0;
? synchronized (lock) {
??? // 我們從文件讀取數(shù)據(jù)
??? // 如果文件不存在,則需要初始化
??? if (!file.exists()) {
????? // 創(chuàng)建一個(gè)新的文件
????? file.createNewFile();
????? count = 0;
??? } else {
????? // 準(zhǔn)備讀取文件
????? FileReader reader = new FileReader(file);
????? // 從文件里讀取int數(shù)據(jù)
????? count = reader.read();
????? // 關(guān)閉文件
????? reader.close();
??? }
??? count++;
??? // 準(zhǔn)備寫(xiě)入文件
??? FileWriter writer = new FileWriter(file);
??? // 寫(xiě)入
??? writer.write(count);
??? // 關(guān)閉文件
??? writer.close();
? }
%>
當(dāng)前一共訪(fǎng)問(wèn)次數(shù)為:<%=count%>
</body>
</html>
?
再次強(qiáng)調(diào),默認(rèn)的文件目錄在tomcat/bin目錄下面。我們修改代碼,將文件放到我們的項(xiàng)目的WEB-INF/classes目錄下面
修改后的代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
?pageEncoding="UTF-8"%>
<%
? // 此文件的版權(quán)歸
? // Java世紀(jì)網(wǎng)(http://www.java2000.net)和 CSDN(www.csdn.net)所有
? // 作者:老紫竹
? // 轉(zhuǎn)載必須保留此版權(quán)聲明
%>
<%@page import="java.io.*"%>
<!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>Insert title here</title>
</head>
<body>
<%!// 一個(gè)同步用的鎖
? private static final Object lock = new Object();
? // 注意默認(rèn)的文件目錄在tomcat/bin目錄下面
? private static File file = null;%>
Hello Word!
<%
? Integer count = 0;
? synchronized (lock) {
??? // 初始化文件名
??? if (file == null) {
????? file = new File(application.getRealPath(".")
????????? + "/WEB-INF/classes/counter.dat");
??? }
??? // 我們從文件讀取數(shù)據(jù)
??? // 如果文件不存在,則需要初始化
??? if (!file.exists()) {
????? // 創(chuàng)建一個(gè)新的文件
????? file.createNewFile();
????? count = 0;
??? } else {
????? // 準(zhǔn)備讀取文件
????? FileReader reader = new FileReader(file);
????? // 從文件里讀取int數(shù)據(jù)
????? count = reader.read();
????? // 關(guān)閉文件
????? reader.close();
??? }
??? count++;
??? // 準(zhǔn)備寫(xiě)入文件
??? FileWriter writer = new FileWriter(file);
??? // 寫(xiě)入
??? writer.write(count);
??? // 關(guān)閉文件
??? writer.close();
? }
%>
當(dāng)前一共訪(fǎng)問(wèn)次數(shù)為:<%=count%>
</body>
</html>
生成的文件如下:
后面我們將使用MySQL數(shù)據(jù)庫(kù)進(jìn)行存儲(chǔ)
?
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
