1
抱歉打擾你們所有人。如何循環存儲在會話中的值?
我有一個關於會議的問題。
在我的情況下,我需要檢查會話是否存儲了值。
如果會話不爲空,我想循環的值做比較。
現在,我只用if語句來檢查book ID的最後一條記錄是否重複。
但我不知道如何使用循環來檢查是否有重複的書籍ID。
有沒有人會爲我提供一些建議來實現這一目標?
非常感謝。
這裏是我的源代碼:
<%@ page contentType="text/html; charset=Big5"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*"%>
<%!
public class BookInfo {
private String id;
private String name;
private int count;
public BookInfo(String id, String name, int count) {
this.id = id;
this.name = name;
this.count = count;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookInfo other = (BookInfo) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public String toString()
{
return " ISBN is " + getId() + " , book title is " + getName() + " & order no. is " + getCount() + "\n";
}
}
%>
<%
try {
List MyCart = (List) request.getSession().getAttribute("MyCart");
if (MyCart == null) {
MyCart = new ArrayList();
}
BookInfo book = new BookInfo("2", "C++ langauge", 1);
//BookInfo book = new BookInfo("2", "java", 1);
if (!MyCart.contains(book)) {
MyCart.add(book);
} else {
//how to loop the value stored in session?
BookInfo tmpBook = (BookInfo) MyCart.get(MyCart.indexOf(book));
tmpBook.setCount(tmpBook.getCount() + book.getCount());
}
request.getSession().setAttribute("MyCart", MyCart);
System.out.println(MyCart);
}
catch (Exception main_error) {
System.out.println("%%%%%%%%%%%"+main_error);
}
%>