如何循环存储在会话的价值? [英] How to loop the value stored in session?

查看:133
本文介绍了如何循环存储在会话的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,打扰大家。

我有一个关于会话的问题。

在我而言,我需要检查会话存储的值或没有。

如果会话不为空,我要循环做的比较值。

现在,我只有在语句来检查书的ID的最后一个记录是重复或不使用。

但我不知道如何使用循环来检查还有就是重复或没有任何一本书的ID。

会不会有人提供了一些建议,让我达致这目标呢?

非常感谢。

这是我的源$ C ​​$ C:

 <%@页面的contentType =text / html的;字符集=中文%GT;
<%@页面进口=java.io. *%>
<%@页面进口=java.sql中。*%GT;
<%@页面进口=javax.sql中。*%GT;
<%@页面进口=java.util中。*%>
<%@页面进口=* java.lang的%GT;
    <%!
    公共类的BookInfo {        私人字符串ID;
        私人字符串名称;
        私人诠释计数;        公众的BookInfo(ID字符串,字符串名称,诠释计数){
            this.id = ID;
            this.name =名称;
            this.count =计数;
        }        公共字符串的getId(){
            返回ID;
        }        公共无效SETID(字符串ID){
            this.id = ID;
        }        公共字符串的getName(){
            返回名称;
        }        公共无效setname可以(字符串名称){
            this.name =名称;
        }        公众诠释的getCount(){
            返回计数;
        }        公共无效setCount(诠释计数){
            this.count =计数;
        }        公共布尔等于(obj对象){
            如果(这种== OBJ)
                返回true;
            如果(OBJ == NULL)
                返回false;
            如果(的getClass()!= obj.getClass())
                返回false;
            其他的BookInfo =(的BookInfo)目标文件;
            如果(ID == NULL){
                如果(other.id!= NULL)
                    返回false;
            }否则如果(!id.equals(other.id))
                返回false;
            返回true;
        }        公共字符串的toString()
        {
            返回书号是+的getId()+,书名就是+的getName()+&放大器;为了不为+ getCount将()+\\ n;
        }
    }
    %GT;    <%
        尝试{
            清单MyCart =(列表)request.getSession()的getAttribute(MyCart);
            如果(MyCart == NULL){
                MyCart =新的ArrayList();
            }            的BookInfo书=新的BookInfo(2,C ++的langauge,1);
            //的BookInfo书=新的BookInfo(2,Java的,1);
            如果(!MyCart.contains(书)){
                MyCart.add(书);            }其他{                //如何循环存储在会话的价值?                的BookInfo tmpBook =(的BookInfo)MyCart.get(MyCart.indexOf(书));
                tmpBook.setCount(tmpBook.getCount()+ book.getCount());            }            。request.getSession()的setAttribute(MyCart,MyCart);
            的System.out.println(MyCart);        }
        赶上(例外main_error){
            的System.out.println(%%%%%%%%%%%+ main_error);
        }
    %GT;


解决方案

您不应该是编写Java code,尤其是在JSP中创建类。它非常糟糕的编码实践 - 尝试提取你的code到实际的java文件。

但是,为了回答你的问题:你应该看到,List对象是可迭代的,这样你就可以通过循环使用它

 为(ABOOK的BookInfo:MyCart){
    // code来看看每个对象的BookInfo这里
}

Sorry to interrupt all of you.

I have a question about session.

In my case, I need to check the session has stored value or not.

If the session is not null, I want to loop the value to do comparison.

Now, I only use if statement to check the last record of book ID is repetitive or not.

But I don't know how to use loop to check there is any book ID that is repetitive or not.

Would any one provide some advice for me to acheive this goal?

Many thanks.

here is my source code:

<%@ 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);
        }
    %>

解决方案

You shouldnt be writing java code, especially creating classes inside a JSP. Its very bad coding practice - try extracting your code to actual java files.

But to answer your question: You should see that the List object is iterable, so you can loop through it using

for(BookInfo aBook: MyCart){
    //code to look at each BookInfo object here
}

这篇关于如何循环存储在会话的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆