对象进入数组JSP(Object into array JSP)

编程入门 行业动态 更新时间:2024-10-27 23:25:34
对象进入数组JSP(Object into array JSP)

我是编程新手,这个问题让我困扰了3天......

我在.jsp网站上有一个帖子表格,用于收集姓名,姓氏,邮件......所有这些信息都保存在对象USER中。 我想在数组中保存用户并将它们显示在同一站点上。 但每次我在表单中使用提交按钮时都会创建新的会话,并且数组输出信息只有一个用户。 我该怎么做才能解决这个问题? 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); } %>

I am new to programming and this problem is bothering me for 3 days straight...

I have a post form on .jsp site for gathering name, surname, mail,... and all this info is saved in object USER. I want to save users in array and display them on the same site. But everytime I use submit button in form new session is created and on array output info is only one user. What should I do to solve this problem? ps: on this stage i can't use sql because it's school projects

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

最满意答案

改变这些线:

... ArrayList seznamUporabnikov = new ArrayList(); //array with i want to display seznamUporabnikov.add(uporabnik); session.setAttribute("seznamUporabnikov", seznamUporabnikov); ... 至 ... 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); ...

Okay after 4 days this stuff works now!!! I am so happy :) anyway.. thank you guys for getting me on the right track...

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);

更多推荐

本文发布于:2023-08-02 12:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   JSP   Object   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!