在Java中将会话值追加到数组
我试图用Java处理会话。每次有人进入一个新页面时,我都需要增加一个视图值,但每个用户每次会话只增加一次。目前,下面的代码没有做到这一点,它一直在增加它。我认为问题可能是每次加载页面时,arraylist都会重新创建?我不知道该怎么处理。因为对象在刷新后不会保留在数组中 处理会话并尝试递增计数的代码 //获取图片的名称 session.setAttributepictureName,pictureName在Java中将会话值追加到数组,java,Java,我试图用Java处理会话。每次有人进入一个新页面时,我都需要增加一个视图值,但每个用户每次会话只增加一次。目前,下面的代码没有做到这一点,它一直在增加它。我认为问题可能是每次加载页面时,arraylist都会重新创建?我不知道该怎么处理。因为对象在刷新后不会保留在数组中 处理会话并尝试递增计数的代码 //获取图片的名称 session.setAttributepictureName,pictureName // create a list of objects that will sto
// 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());
}
}
更新的justs每个会话更新一张图片,而不是每个会话更新所有访问的图片
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");
}
}
您必须在会话中存储已访问页面的列表 以下是流程: 尝试从myVisitedPages的会话中获取属性 第一次它将是空的。创建一个新的ArrayList,使用setAttribute将其附加到会话,并向其中添加第一个页面id。 第二次以后,该列表将作为会话属性提供,您可以获取该列表,并在该列表中进行检查。 顺便说一句,我不知道为什么您当前访问的页面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");
}
好吧,据我所知,你也想要 每次网上有新的访问,柜台都会关闭 看法 如果是的话。 宣布
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);
}
您是将这个命中计数器存储在内存中还是数据库中?是的,存储在mysql数据库中。我将用我正在使用的方法更新OP。谢谢你的回复。我已经用代码更新了我的OP,以显示我的尝试。目前有一些改进,但现在每个会话只更新一张图片。很难用一段代码进行调试。如果没有控制台输出,则更加困难。我猜你每次都会有一次新的训练。尝试打印会话对象本身。可能每次都会显示一个新的哈希代码,或者您可能总是得到相同的pictureName属性。您也可以尝试登录。非常感谢您的回复。所以我已经打印出了这两个属性。但是sessionId在整个过程中与您预期的一样,并且pictureName属性正在为每个页面打印正确的pictureName。@JohnDoi2021您遗漏了一件小事。更新了我的答案。请花一分钟看看如何正确设置代码和文本的格式。