该程序实现一个计数器,在此程序中要使用一个文件count.txt我不知道要放这个文件的路径.麻烦高手指点一下.
该程序现在存放得路径为:D:\tomcat\webapps\test
请问count.txt文件应该放在哪里呢?<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>文字模式访问记数器</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="#ffffff">
<%@ page import = "java.io.*"%>
<%
BufferedReader file;
String countFile = "count.txt";
file = new BufferedReader(new FileReader(countFile));
String readStr = null;
int writeStr=1;
try{
readStr = file.readLine();
}
catch(IOException e){
System.out.println("读取数据错误!");
}
if(readStr == null)
readStr="没有任何记录";
else{
writeStr = Integer.parseInt(readStr)+1;
}
try
{
PrintWriter pw;
pw = new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e){
out.println(e.getMessage());
}
%>
<p align = "center">
<H1>文字模式访问记数器</H1>
<H3>你是本网页第
<font color = "ff0000" size = "7">
<%=readStr%>
</font>名访客</H3>
</body>
</html>
该程序现在存放得路径为:D:\tomcat\webapps\test
请问count.txt文件应该放在哪里呢?<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>文字模式访问记数器</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="#ffffff">
<%@ page import = "java.io.*"%>
<%
BufferedReader file;
String countFile = "count.txt";
file = new BufferedReader(new FileReader(countFile));
String readStr = null;
int writeStr=1;
try{
readStr = file.readLine();
}
catch(IOException e){
System.out.println("读取数据错误!");
}
if(readStr == null)
readStr="没有任何记录";
else{
writeStr = Integer.parseInt(readStr)+1;
}
try
{
PrintWriter pw;
pw = new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e){
out.println(e.getMessage());
}
%>
<p align = "center">
<H1>文字模式访问记数器</H1>
<H3>你是本网页第
<font color = "ff0000" size = "7">
<%=readStr%>
</font>名访客</H3>
</body>
</html>
解决方案 »
- 【内有福利】百度自动回帖,怎么制作模拟提交?
- 页面不能显示数据
- orcale 10g在XP下安装失败问题
- Sruts验证框架
- java.sql.SQLException: ORA-29538: Java not installed
- hibernate的问题,求解 “Not binding factory to JNDI, no JNDI name configured”
- jsp+struts+mysql 做成象google搜索栏一样的效果
- struts.xml 配置文件读取
- 向JSP/JAVA 爱好者推荐500篇实用的文章(特别适合初学
- 如何实现树型结构?
- 运行用struts+hibernate做的项目要怎么配置么
- ********早发现早收获,java就业最权威的指引**********************
支持的应该是绝对路径吧...改下程序试试:
//String countFile = "count.txt";
String countFile = request.getRealPath("count.txt");