Java 对象转换为数组JSP

Java 对象转换为数组JSP,java,jsp,Java,Jsp,我是编程新手,这个问题连续三天困扰着我 我在.jsp网站上有一个收集姓名、姓氏、邮件等信息的帖子表单,。。。所有这些信息都保存在object USER中。我想将用户保存在数组中,并在同一站点上显示它们。但每次我在表单中使用submit按钮时,都会创建一个新会话,并且阵列上的输出信息只有一个用户。 我该怎么解决这个问题? ps:在这个阶段我不能使用sql,因为这是学校的项目 <% Uporabnik uporabnik = new Uporabnik(); //user uporabn

我是编程新手,这个问题连续三天困扰着我

我在.jsp网站上有一个收集姓名、姓氏、邮件等信息的帖子表单,。。。所有这些信息都保存在object USER中。我想将用户保存在数组中,并在同一站点上显示它们。但每次我在表单中使用submit按钮时,都会创建一个新会话,并且阵列上的输出信息只有一个用户。 我该怎么解决这个问题? ps:在这个阶段我不能使用sql,因为这是学校的项目

<% Uporabnik uporabnik = new Uporabnik(); //user
   uporabnik.setIme(request.getParameter("ime"));
   uporabnik.setPriimek(request.getParameter("priimek"));
   uporabnik.setEmail(request.getParameter("email"));
   uporabnik.setKraj(request.getParameter("kraj"));
   uporabnik.setPostnaStevilka(request.getParameter("postnaStevilka"));

   ArrayList<Uporabnik> seznamUporabnikov = new ArrayList<Uporabnik>(); //array with i want to display
   seznamUporabnikov.add(uporabnik);
   session.setAttribute("seznamUporabnikov", seznamUporabnikov); %>

   <form method="post" action="Registracija.jsp">
     Ime: <input type="text" name="ime"/> <br/>
     Priimek: <input type="text" name="priimek"/> <br/>
     Email: <input type="text" name="email"/> <br/>
     Kraj: <input type="text" name="kraj"/> <br/>
     Postna stevilka: <input type="text" name="postnaStevilka"/> <br/>
     <input type="submit" name="potrdi" value="Vnesi">
     <input type="reset" name="tabelaReset" value="Izbrisi iz tabele">
     <input type="submit" name="resetiraj" value="Izbrisi podatke">
   </form>

   <br/> Seja: <%=session.getAttribute("Oseba")%> <hr/>

   <% if (request.getParameter("potrdi")!=null) {
         session.setAttribute("Oseba", uporabnik);
      } %>
   <% if (request.getParameter("resetiraj")!=null) {
         session.setAttribute("Oseba", null);
      } %>

输入法:
普里梅克:
电子邮件:
Kraj:
Postna stevilka:

Seja:
更改这些行:
。。。
ArrayList seznamUporabnikov=新的ArrayList()//我要显示的数组
seznamUporabnikov.add(uporabnik);
session.setAttribute(“seznamUporabnikov”,seznamUporabnikov);
…
。。。
ArrayList seznamUporabnikov=session.getAttribute(“seznamUporabnikov”);
if(seznamUporabnikov==null){//在创建之前检查是否已经在会话中。
ArrayList seznamUporabnikov=new ArrayList();//要显示的数组
}
seznamUporabnikov.add(uporabnik);
session.setAttribute(“seznamUporabnikov”,seznamUporabnikov);

…创建一个类,在该类下创建一个静态用户列表,并将所有用户添加到该列表中
此列表将在应用程序的整个生命周期中提供。

4天后,此功能现在可以使用了!!!我很高兴:)无论如何。。谢谢你们让我走上正轨

ArrayList<Uporabnik> seznamUporabnikov=null;
//check if already in session before creating.  
if(session.getAttribute("seznamUporabnikov") == null) {        
    seznamUporabnikov = new ArrayList<Uporabnik>(); 
    //array which I want to display
    session.setAttribute("seznamUporabnikov", seznamUporabnikov);   
} else {
    seznamUporabnikov = 
                 (ArrayList<Uporabnik>)session.getAttribute("seznamUporabnikov");
}

if (request.getParameter("potrdi") != null) {
    seznamUporabnikov.add(uporabnik);
}


session.setAttribute("seznamUporabnikov", seznamUporabnikov);
ArrayList seznamUporabnikov=null;
//在创建前检查是否已在会话中。
if(session.getAttribute(“seznamUporabnikov”)==null){
seznamUporabnikov=新阵列列表();
//我要显示的数组
session.setAttribute(“seznamUporabnikov”,seznamUporabnikov);
}否则{
塞兹纳穆波拉布尼科夫=
(ArrayList)session.getAttribute(“seznamUporabnikov”);
}
if(request.getParameter(“potrdi”)!=null){
seznamUporabnikov.add(uporabnik);
}
session.setAttribute(“seznamUporabnikov”,seznamUporabnikov);

您将继续创建新阵列。相反,从会话中检索现有数组,并将
uporabnik
添加到现有的array.registicija.jsp,可能有问题,因为表单提交时有此urljsp@AlpeshGediya或者OP每次都在创建一个新数组,并将其设置到会话中,其中每次都会添加一个新数组并将其设置到会话中。@DaveNewton,是的,这也是可能的,希望您不介意您的姓氏暗示,“你是伊萨克·牛顿爵士的后裔吗?”据我们所知,阿尔皮什基迪亚不是;我们可以追溯到大约那个时候,所以如果有一种关系,它很可能是从很久以前开始的:)只有当他们不希望每个用户都使用它时,它才起作用,并且它是同步的,等等。直到OP声明,否则,答案似乎应该与保持列表在会话中有关。由于这个问题是显而易见的,我看不出有任何理由保持在应用范围内。 ... ArrayList seznamUporabnikov=session.getAttribute("seznamUporabnikov"); if(seznamUporabnikov == null) { //check if already in session before creating. ArrayList seznamUporabnikov = new ArrayList(); //array with i want to display } seznamUporabnikov.add(uporabnik); session.setAttribute("seznamUporabnikov", seznamUporabnikov); ...
ArrayList<Uporabnik> seznamUporabnikov=null;
//check if already in session before creating.  
if(session.getAttribute("seznamUporabnikov") == null) {        
    seznamUporabnikov = new ArrayList<Uporabnik>(); 
    //array which I want to display
    session.setAttribute("seznamUporabnikov", seznamUporabnikov);   
} else {
    seznamUporabnikov = 
                 (ArrayList<Uporabnik>)session.getAttribute("seznamUporabnikov");
}

if (request.getParameter("potrdi") != null) {
    seznamUporabnikov.add(uporabnik);
}


session.setAttribute("seznamUporabnikov", seznamUporabnikov);