【问题标题】:Appending session values to an Array in Java将会话值附加到 Java 中的数组
【发布时间】:2018-11-16 23:36:24
【问题描述】:

我正在尝试处理 Java 中的会话。每次有人进入新页面时,我都需要增加视图值,但每个用户每次会话只增加一次。目前下面的代码并没有这样做,它一直在增加它。我认为问题可能是每次加载页面时,arraylist 都会再次新建?我不确定如何处理这个问题。因为刷新后对象不会留在数组中。

处理会话并尝试增加计数的代码 // 获取图片的名字 session.setAttribute("pictureName", pictureName);

    // create a list of objects that will store all the pages a user visits
    ArrayList<String> listOfObjects = new ArrayList<String>();

    // make a image_id variable but assign it the value of the page name
    String image_id = (String) session.getAttribute("pictureName");

    // go and compare each element in the list, to the id.
    // This should NOT match on the first viewing since it's the first time the user
    // visits the page
    // this should then increment the view count
    if (Utilities.isFirstVisit(listOfObjects, image_id) == true) {
        Utilities.IncreaseCount(out, pictureName);
        out.println("its worked");

    } else {
        // if a match is found then the user has already visited this page
        // therefore don't increment the view count for the love of christ
        out.println("A match was found");

    }

调用的方法将对象添加或不添加到数组列表中

public static boolean isFirstVisit(ArrayList <String> listOfObjects,
            String image_id) {
        System.out.println(listOfObjects);
        System.out.println(image_id);

    if(listOfObjects.contains(image_id)) {
        System.out.println("It does contain");
        System.out.println(image_id);
        return false;
    }

    else { 
        listOfObjects.add(image_id);
        System.out.println("It does not contain");
        System.out.println(image_id);
        return true;
        }
    }

增加浏览量的方法

//Increase the access count on the picture
    public static void IncreaseCount(PrintWriter out, String pictureName) {

        Connection con = null;
        try { // Connect to the database
            con = openConnection(out);
        } catch (Exception e) { // Failed to open the connection
            out.println("<P>" + e.getMessage());
            return;
        }

        try {

            Statement stmt = con.createStatement();
            String query;
            ResultSet rs1;

            query = "UPDATE Statistics SET AccessCount = AccessCount + 1 WHERE PictureNo = (SELECT PictureNo FROM Pictures WHERE FileName = '"
                    + pictureName + "')";

            stmt.executeUpdate(query);

            stmt.close();

        } catch (SQLException ex) {
            out.println("<P>SQLException: " + ex.getMessage());
        }

    }

#

更新只是在每个会话中更新一张图片,而不是在每个会话中更新所有访问过的图片

   if(session.getAttribute("myVisitedPages") == null) {
           ArrayList<String> listOfPages = new ArrayList<String>();

           session.setAttribute("myVisitedPages", listOfPages);

            if (Utilities.isFirstVisit(listOfPages, pictureName) == true) {
                Utilities.IncreaseCount(out, pictureName);
                out.println("its worked");

            } 
       }

【问题讨论】:

  • 您是将这个命中计数器存储在内存中还是某个数据库中?
  • 在 mysql 数据库上是。我将使用我正在使用的方法更新 OP。

标签: java


【解决方案1】:

您必须在会话中存储访问过的页面列表。

流程如下:

  1. 尝试从“myVisitedPages”会话中获取属性
  2. 第一次是null。创建一个new ArrayList(),使用setAttribute 将其附加到会话中,并为其添加第一个页面ID。
  3. 第二次以后,该列表将作为会话属性提供,您可以获取该列表并查看该列表。

(顺便说一句,我不确定为什么您当前访问的页面 ID 需要出现在会话中。)

更新后。 If 不应该嵌套:

  ArrayList<String> listOfPages = session.getAttribute("myVisitedPages");
  if(listOfPages == null) {
       listOfPages = new ArrayList<String>();

       session.setAttribute("myVisitedPages", listOfPages);
   }

   if (Utilities.isFirstVisit(listOfPages, pictureName) == true) {
       Utilities.IncreaseCount(out, pictureName);
       out.println("its worked");
   } 

【讨论】:

  • 感谢您的回复。我已经用代码更新了我的 OP 以显示我尝试过的内容。目前有一些改进,但现在每个会话只更新一张图片。
  • 很难用一段代码来调试。没有控制台输出就更难了。我猜你每次都会得到一个新的会话。尝试打印会话对象本身。也许它每次都会显示一个新的哈希码。
  • 或者你可能总是得到相同的图片名称属性。您也可以尝试记录。
  • 非常感谢您的回复。所以我打印了这两个属性。但是 sessionId 与您期望的一样对于整个事情保持不变,并且图片名称属性正在为每个页面打印出正确的图片名称。
  • @JohnDoi2021 你错过了一件小事。更新了我的答案。
【解决方案2】:

好吧,据我了解,你也想增加 每次有新访问时的计数器也是网络 看法 如果是这样。 声明一个

HttpSession obj = request.getSession();
obj.setSessionAttribute("image_id", null);
  
  If (obj.getAttribute("image_id") == null){
     Write_to_db();
     string value="user counted";
     obj.setSessionAttribute("image_id", value); 
     }

【讨论】:

  • 请花一点时间看看如何正确格式化您的代码(和文本)。
猜你喜欢
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2016-07-22
  • 2018-01-08
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多