Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于ArrayList上的循环,请更改对象_Java_Jsp_Servlets_Arraylist_Request - Fatal编程技术网

Java 对于ArrayList上的循环,请更改对象

Java 对于ArrayList上的循环,请更改对象,java,jsp,servlets,arraylist,request,Java,Jsp,Servlets,Arraylist,Request,我有一个jsp页面(上面写着,MyJspPage.jsp)- 应该注意的是,当我从MyJspPage.jsp中省略for循环并将其更改为- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <% ArrayList<Person> ownerList = (ArrayList

我有一个jsp页面(上面写着,
MyJspPage.jsp
)-

应该注意的是,当我从MyJspPage.jsp中省略for循环并将其更改为-

   <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <%
        ArrayList<Person> ownerList = (ArrayList<Person>) request
                .getAttribute("ownerList");
        //set again ..
            request.setAttribute("ownerList",ownerList) ;   

    %>
    </head>
    <body>
                <form action="servlet123" method="POST">
                       // some fields .. 
                       <input type="submit" value="join" />
        </form>

    </body>
    </html>

//有些领域。。
MyJspPage.jsp
servlet123
之间的所有关系都可以正常工作

这是一种方法

JSP代码如下所示


不必在请求中再次设置arraylist,您可以在会话中设置它,如下所示
session.setAttribute(“ownerList”,ownerList)

在for循环中使用arraylist之前,可以检查它是否不为null

        if (ownerList != null)
        {
          for (Person person : ownerList) {
    %>
                // some HTML code.. 
    <%
          }
        }
    %>
if(所有者列表!=null)
{
for(个人:所有者列表){
%>
//一些HTML代码。。
在servlet中,您可以将代码编写为

 HttpSession session = request.getSession(false);
 ArrayList<Person> ownerList = (ArrayList<Person)session.getAttribute("ownerList"); 
 request.setAttribute("ownerList", ownerList);
 session.setAttribute("ownerList", null); // toremove unnecessary code from the session
HttpSession session=request.getSession(false);

ArrayList所有者列表=(ArrayList首先检查ownerList是否为null,确保它已传递给您的pageRequest对象。对于每个请求,该对象都是新的。您应该从会话中保存和检索该列表。@bHeshgurhung是否有任何方法可以根据请求执行此操作?是-对于每个提交,该请求对象都是新创建的。另外,上一个请求对象是d。)JSP完全呈现后,将执行estroyed。
   <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <%
        ArrayList<Person> ownerList = (ArrayList<Person>) request
                .getAttribute("ownerList");
        //set again ..
            request.setAttribute("ownerList",ownerList) ;   

    %>
    </head>
    <body>
                <form action="servlet123" method="POST">
                       // some fields .. 
                       <input type="submit" value="join" />
        </form>

    </body>
    </html>
        if (ownerList != null)
        {
          for (Person person : ownerList) {
    %>
                // some HTML code.. 
    <%
          }
        }
    %>
 HttpSession session = request.getSession(false);
 ArrayList<Person> ownerList = (ArrayList<Person)session.getAttribute("ownerList"); 
 request.setAttribute("ownerList", ownerList);
 session.setAttribute("ownerList", null); // toremove unnecessary code from the session