我正在尝试处理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());
}
}
Updated只是每会话更新一张图片,而不是每会话更新所有访问过的图片
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");
}
}
答案 0 :(得分:0)
您必须在会话中存储访问页面的列表。
这是流程:
null
。创建一个new ArrayList()
,使用setAttribute
将其附加到会话上,然后向其添加首页ID。(顺便说一句,我不确定为什么您当前访问的页面ID需要在会话中存在。)
更新后。如果不应该嵌套:
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");
}
答案 1 :(得分:-1)
好吧,据我所知,你想要太多增量 每次有新访问时的柜台也是网络 看法 如果是这样的话。 声明一个
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);
}