求助!急!高分送出.关于制做个人网站的两个问题.
本人想用JSP+tomcat+Apache做一个个人网站,遇到两个问题,一个是记录访问量问题,刷新不能改变访问量,还有我准备放个下载区,可以让访问者可以下载上面的资源,请问怎么做啊?有谁给我个完整的方案.我定高分送出.发我Email就行了,我的Email为:jrs110509@yahoo.com.cn
------解决方案--------------------服务器启动时(实现ServletContextListener监听器contextInitialized方法),读取数据库,并将其用一个计数变量保存在application范围内
session创建时(实现HttpSessionListener监听器sessionCreated方法),读取计数变量加1并重新保存
服务器关闭时(实现ServletContextListener监听器contextDestroyed方法),更新数据库
------解决方案--------------------String dir = getServletContext().getRealPath( "/ ");
if (session.isNew()) {
String nameOfTextFile = "peopleCount.txt ";
nameOfTextFile = dir+ nameOfTextFile;
BufferedReader file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr = null;
try {
readStr = file.readLine();
} catch (
IOException e) {
System.out.println( "读取数据失败! "+e.getMessage());
}
if(readStr!=null){
try{
int number = Integer.parseInt(readStr);
number++;
String str = String.valueOf(number);
writeStr=str;
session.setAttribute( "count ", str);
try{
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(number);
pw.close();
} catch (IOException e) {
//out.println(e.getMessage());
System.out.println( "写数据失败! ");
}
}catch(
NumberFormatException e){
System.out.print( "数据类型转换错误! ");
}
}
}