简体   繁体   English

将 arrayList 从 jsp 发送到 servlet

[英]Send an arrayList from a jsp to a servlet

The purpose of this project is to move the objects from the arrayLists, and also erasing them.该项目的目的是从 arrayLists 中移动对象,并擦除它们。 I have made three arrayLists on the servlet, these arrayList are the ones thar I want to work with, I've already know how to show them in the JSP page, and also how to move and erase them, but my problem is that I want to be able to retrieve the current state of the arrayList objects because the element and moving and erasing that I want to do is through some buttons that I've created.我在 servlet 上创建了三个数组列表,这些 arrayList 是我想要使用的,我已经知道如何在 JSP 页面中显示它们,以及如何移动和擦除它们,但我的问题是我希望能够检索 arrayList 对象的当前 state 因为我想要做的元素以及移动和擦除是通过我创建的一些按钮。 So I've tried creating the buttons inside a form referenced to the jsp servlet, but when I press the buttons doesn't show anything, I've used a System.out.println() to be able to recover the value, from the JSP, but I don't get anything.因此,我尝试在引用 jsp servlet 的表单中创建按钮,但是当我按下按钮时没有显示任何内容,我使用了 System.out.println() 能够从JSP,但我什么也没得到。

This is the servlet:这是小服务程序:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    // response.getWriter().append("Served at: ").append(request.getContextPath());
    System.out.println("Dentro Servlet");
    String borrar = request.getParameter("borrar");
    String btn1 = request.getParameter("button1");
    System.out.println("El btn1 devuelve: " + btn1);

    ArrayList<ObjetosArreglo> personas = new ArrayList<ObjetosArreglo>();
    personas.add(new ObjetosArreglo("Cesar"));
    personas.add(new ObjetosArreglo("Miguel"));
    personas.add(new ObjetosArreglo("Chavez"));
    personas.add(new ObjetosArreglo("Linares"));
    personas.add(new ObjetosArreglo("TATAMASTER"));

    ArrayList<ObjetosArreglo> mascotas = new ArrayList<ObjetosArreglo>();
    mascotas.add(new ObjetosArreglo("Clifford"));
    mascotas.add(new ObjetosArreglo("Chuzo"));
    mascotas.add(new ObjetosArreglo("Hoshi"));
    mascotas.add(new ObjetosArreglo("Persian"));
    mascotas.add(new ObjetosArreglo("Bebeto"));
    
    ArrayList<ObjetosArreglo> comidas = new ArrayList<ObjetosArreglo>();
    comidas.add(new ObjetosArreglo("Carne"));
    comidas.add(new ObjetosArreglo("Queso"));
    comidas.add(new ObjetosArreglo("Yogur"));
    comidas.add(new ObjetosArreglo("Pan dulce"));
    comidas.add(new ObjetosArreglo("Pasta"));
    
    if ("1".equals(borrar)) {
        MetodosUtiles.borrarElemento(personas, 1);
    }
    if (request.getParameter("button1")!= null) {
        System.out.println("Button 1"+request.getParameter("button1"));
        MetodosUtiles.borrarPriElemento(personas);
    }else if (request.getParameter("button2")!=null) {
        MetodosUtiles.moverUltPriElemento(personas, comidas);
    } else if (request.getParameter("button3")!= null) {
        MetodosUtiles.moverPriElemento(comidas, mascotas);
    }

    request.setAttribute("ArrayCom", comidas);      
    request.setAttribute("ArrayMasc", mascotas);
    request.setAttribute("ArrayPer", personas);
    request.getRequestDispatcher("index.jsp").forward(request, response);

}

This is the methods class:这是方法 class:

    public static void moverUltElemento(ArrayList elem1, ArrayList elem2) {
//Copiar último Elemento de un ArrayList a otro.
        elem2.add(elem1.get(elem1.size() - 1));
        elem1.remove(elem1.size() - 1);
    }
    
    public static void moverUltPriElemento(ArrayList elem1, ArrayList elem2) {
        //Copiar último Elemento de un ArrayList a otro.
                elem2.add(0, elem1.get(elem1.size() - 1));
                elem1.remove(elem1.size() - 1);
            }

    public static void moverPriElemento(ArrayList elem1, ArrayList elem2) {
//mover primer elemento al array de abajo 
        elem1.add(0, elem2.get(0));
        elem2.remove(elem2.get(0));
    }
    

    // Borrar elemento
    public static void borrarElemento(ArrayList elem1, int index) {
        elem1.remove(elem1.get(index));
    }

    // Eliminar último elemento del ArrayList
    public static void borrarUltElemento(ArrayList elem1) {
        elem1.remove(elem1.size() - 1);
        System.out.println("ArrayList sin el último elemento: " + elem1);
    }

    // Eliminar primer elemento del ArrayList
    public static void borrarPriElemento(ArrayList elem1) {
        elem1.remove(0);
        System.out.println(elem1);
    }

//////////////////////////////////////////////////////////////////////////////////////////////////////

And this is the JSP:这是 JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ejercicio Arreglos JSP</title>
</head>
<body>
    <%@page import="java.util.ArrayList"%>
    <%--Importing all the dependent classes--%>
    <%@page import="clase.ObjetosArreglo"%>
    <%@page import="java.util.Iterator"%>
    <!-- Arreglo Personas -->
    <%
        ArrayList<ObjetosArreglo> personasList = (ArrayList) request.getAttribute("ArrayPer");
    %>
    <%--Assigning ArrayList object containing Employee data to the local object --%>

    <strong> <a href="<%=request.getContextPath()%>/arrays">Mostrar
            Datos</a> <br> <a
        href="<%=request.getContextPath()%>/arrays?borrar=1">Borrar Datos</a>
    </strong>
    <form action="arrays" method="post">
        <input type="button" name="button1" value="Button 1" /> 
        <input type="submit" name="button2" value="Button 2" />
        <input type="submit" name="button3" value="Button 3" />
    </form>
    <br></br>
    <div style="text-align: center;">
        <table cellspacing="2" cellpadding="2">
            <tr>
                <%
                    // Iterating through subjectList
                    ArrayList<ObjetosArreglo> arre = (ArrayList) request.getAttribute("ArrayPer");
                    if (arre != null) // Null check for the object
                    {
                        for (ObjetosArreglo iter : arre) {
                %>
                <td><pre><%=iter.getNombre()%>             </pre></td>
                <%
                    }
                %>
            </tr>
            <%
                }
            %>
            <!-- Arreglo Mascotas -->
            <%
                ArrayList<ObjetosArreglo> mascotasList = (ArrayList) request.getAttribute("ArrayMasc");
            %>
            <%--Assigning ArrayList object containing Employee data to the local object --%>
            <tr>
                <%
                    // Iterating through subjectList
                    ArrayList<ObjetosArreglo> arre1 = (ArrayList) request.getAttribute("ArrayMasc");
                    if (arre1 != null) // Null check for the object
                    {
                        for (ObjetosArreglo iter : arre1) {
                %>
                <td><pre><%=iter.getNombre()%>             </pre></td>
                <%
                    }
                %>
            </tr>
            <%
                }
            %>
            <!-- Arreglo Mascotas -->
            <%
                ArrayList<ObjetosArreglo> comidasList = (ArrayList) request.getAttribute("ArrayCom");
            %>
            <%--Assigning ArrayList object containing Employee data to the local object --%>
            <tr>
                <%
                    // Iterating through subjectList
                    ArrayList<ObjetosArreglo> arre2 = (ArrayList) request.getAttribute("ArrayCom");
                    if (arre2 != null) // Null check for the object
                    {
                        for (ObjetosArreglo iter : arre2) {
                %>
                <td><pre><%=iter.getNombre()%>             </pre></td>
                <%
                    }
                %>
            </tr>
            <%
                }
            %>
        </table>
    </div>
</body>
</html>

Insted of request you can try using cookies and save the values of array in it - they will be availabe for you as long as the session lasts, so basically until you close your program.根据要求,您可以尝试使用 cookies 并将数组的值保存在其中 - 只要 session 持续存在,它们就会为您提供,所以基本上直到您关闭程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM