Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java&;servlet。。?_Java_Servlets - Fatal编程技术网

如何在java&;servlet。。?

如何在java&;servlet。。?,java,servlets,Java,Servlets,我需要显示网站访问者数量,如下所示: 访客总数 当天的访客总数 我已经完成了第一个要求。如何在每天的基础上实施第二个计划 下面是Servlet代码: public class HitCounterServlet extends HttpServlet { String fileName = "D://hitcounter.txt"; long hitCounter; protected void doGet(HttpServletRequest reques

我需要显示网站访问者数量,如下所示:

  • 访客总数
  • 当天的访客总数
  • 我已经完成了第一个要求。如何在每天的基础上实施第二个计划

    下面是Servlet代码:

       public class HitCounterServlet extends HttpServlet {
    
         String fileName = "D://hitcounter.txt";
         long hitCounter;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        readFile();
        updateHitCounterFile();
        HttpSession usersession = request.getSession();
        usersession.setAttribute("HITCOUNTER", hitCounter);
    }
    
    private void updateHitCounterFile() throws IOException {
    
        /**
         * Here I am increasing counter each time this HitCounterServlet is called. 
         * I am updating hitcounter.txt file which store total number of visitors on website.
         * Now I want total number of visitor on per day basis. 
        */
    
        hitCounter = hitCounter + 1;
    
        // read and update into file
        File file = new File(fileName);
    
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
    
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(Long.toString(hitCounter));
        bw.close();
    }
    
    public void readFile() {
        BufferedReader br = null;
        String temp = "";
        try {
            br = new BufferedReader(new FileReader(fileName));
            while ((temp = br.readLine()) != null) {
                hitCounter = Long.parseLong(temp);
            }
            System.out.println("HIT Counter : " + hitCounter);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
     }
    }
    

    您应该使用数据库并使用当前日期更新计数器


    或者为当前日期文件创建单独的文件当前日期文件名,如
    10-23-1998.txt
    &更新当天的计数器。希望有帮助。

    声明一个变量计数器并在JSP中递增。

    当部署JSP并准备好其java文件时,该变量应被视为该java文件中的静态变量。所以,即使重新加载文件,计数器也会递增

    这只适用于所有柜台。 如果重新部署,此值将丢失。然后是序列化或DB选项


    或者在web.xml中使用servlet init config参数。我目前没有与JSP联系。它的名称听起来和前面提到的一样。

    您不应该在会话中设置计数值,它应该在应用程序上下文中设置(在应用程序范围内的一个变量中)是的,谢谢您的好主意基于日期创建新文件,但是服务器上每天创建的文件太多。@dev,没有问题。为当天创建文件时,请检查前一天的文件是否存在,如果存在,则删除它或仅使用cron作业定期删除该文件
    Counter   Date
    444       10-23-1998
    555       10-24-1998