当前位置:编程学习 > JAVA >>

新手学javaweb,请大侠帮忙看看代码错误

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.math.BigInteger"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.PrintStream"%>
<%@page import="java.io.FileOutputStream"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>网站计数器</title>
  </head>
  
  <body>
  <%!
      BigInteger count = null;
  %>
  <%!
      public BigInteger load(File file){
          BigInteger count = null;
          try{
              if(file.exists()){
                  Scanner scan = new Scanner(new FileInputStream(file));
                  if(scan.hasNext()){
                      count = new BigInteger(scan.next());
                  }
                  scan.close();
              }else{
                  count = new BigInteger("0");
                  save(file, count);
              }
          }catch (Exception e){
              System.out.println(e);
          }
          return count;
      }
      public void save(File file, BigInteger count){
          try{
              PrintStream ps = null;
              ps = new PrintStream(new FileOutputStream(file));
              ps.println(count);
              ps.close();
          }catch (Exception e){
              System.out.println(e);
          }
      }
  %>
  <%
      System.out.println("count=========" + count);
      String fileName = this.getServletContext().getRealPath("/") + "count.txt";
      File file = new File(fileName);
      if(session.isNew()){
          synchronized(this){
              count = load(file);
              System.out.println("count=========" + count);
              count = count.add(new BigInteger("1"));
              save(file, count);
              System.out.println("count=========" + count);
          }
      }
  %>
  <h2>您是第<%=count == null ? 0 : count %>位访客</h2>
  </body>
</html>



为什么红色字体处显示有错误呢?? --------------------编程问答-------------------- 老大,你这初始化的时候是空的吧?BigInteger count = null;
-->count = count.add(new BigInteger("1"));
--------------------编程问答-------------------- 错误提示是什么? --------------------编程问答-------------------- BigInteger count = null;
你在这儿设置为空值,后面也要给赋值的。不然怎么实现计数的操作呢。 如何修改?敬请期待高人~~~ --------------------编程问答--------------------
正常操作,第一次创建文件,而后进入exists 里面,里面已经写入值,不会造成问题吧,除非文件创建后,里面为空。[1,检查部署文件里面是否存在一个空的count.txt 2,代码完善判断条件]
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,