现在的位置: 首页 > 综合 > 正文

页面显示动画数字

2012年04月20日 ⁄ 综合 ⁄ 共 1557字 ⁄ 字号 评论关闭

 页面显示动画数字

Count.java

package com.lpy;
import java.io.*;
public class Count {
	
	/*
	 * 把count写到filename文件里
	 */
	public static void writeFile(String filename,long count){
		try {
			PrintWriter out = new PrintWriter(new FileWriter(filename));
			out.println(count);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 从filename文件里读出count,并返回
	 */
	public static long readFile(String filename){
		File f = new File(filename);
		long count = 0;
		
		if(!f.exists()){	//如果文件不存在,调用writeFile,并入0
			writeFile(filename,0);
		}
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
				count = Long.parseLong(in.readLine());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return count;
	}
	
	/*
	 * 将count转成字符串读出来 写进<img >标签里替换成文件名为0、1、2……的图片,并按位显示到页面上
	 */
	public static String tranform(long count){
		String strcount = ""+count;
		String newString = "";
		for(int i = 0;i<strcount.length();i++){
			newString += "<img src='images\\" + strcount.charAt(i) + ".gif' >";
		}
		return newString;
	}
}

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.lpy.Count"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>My JSP 'count.jsp' starting page</title>

  </head>
  <% 
  long count = Count.readFile(request.getRealPath("/") + "/count.txt");
 // session.removeAttribute("session");
  if(session.getAttribute("session")==null){
		session.setAttribute("session","y");
 		session.setMaxInactiveInterval(5);
  
  count+=1;
  Count.writeFile(request.getRealPath("/") + "/count.txt",count);
  }
  %>
  <body>

    本WEB访问过 <%=Count.tranform(count)%> 次!!<br/><hr><br/>
    本WEB访问过 <%=count%> 次!!
  </body>
</html>

 

0.gif1.gif2.gif3.gif4.gif5.gif6.gif7.gif8.gif9.gif

抱歉!评论已关闭.