How to send an array from a servlet to a jsp by means of a drop-down-list

0

You will see I have a problem, I am doing a program that through a page jsp that contains a textbox to enter a number that will be the total number of fibonacci series that the user will see. That is, if you enter 5, you will only see 5 numbers of the fibonacci series. and that at the moment of giving the button calculate, send the number to a servlet that does the operation and respond to the jsp showing a drop-down-list with the series of fibonacci numbers, my problem is that I do not have the slightest idea how to return the array to jsp ... for the moment I have the following:

IN MY JSP:      

FIBONACCI SERIES

     Enter a number to see the fibonacci series of it:
        <!-- form que contiene los datos a mandar al servlet -->
        <form name="idFibonacci" action="srvFibo" method="POST">
            <input type="text" name="txtNum" value="" placeholder="INGRESE EL NUMERO"/>
            <input type="submit" value="Calcular" name="btnCalcular"/> <!-- boton que al apretar manda datos al servlet-->
        </form>

By clicking on the calculate button, send the data to the servlet, which does the following:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        RequestDispatcher r = null;

        //si el no se ha aparetado el boton y la caja de texto no esta vacia
        if((request.getParameter("btnCalcular")!=null) && (request.getParameter("txtNum")!=null))
        {
            //hago las variables para calcular el fibonacci
            int resultado=0, a=0, b=1;
            int n = Integer.parseInt(request.getParameter("txtNum")); //capturo el numero del textbox del jsp
            int array[] = new int[n];//declaro mi array con el numero de espacios que mando el usuario en la variable n

            for(int i=1; i<=n; i++)//hago el procedimiento para calcular el fibonacci
            {
                a=b;
                b=resultado;
                resultado = a+b;
                array[i]=resultado;//guardo los resultados en las posiciones del array
                request.setAttribute("fibonacci",array[i]);//segun yo aqui se guarda cada posicion en la variable fibonacci
                r = request.getRequestDispatcher("fibo.jsp");//lo manda al jsp

            }

            r.forward(request, response);
        }


    }
}

Finally it goes back to the jsp and it would have to print in a drop-down-list but I do not know how to do it ... see:

 <%
            //si la respuesta de mi servlet en fibonacci es diferente de null
            if(request.getAttribute("fibonacci")!=null)
            {//abajo en el drop down list tendría que imprimir la serie del fibonacci del array pero no se comoo hacerle 
        %>
                <select name="dropSerie">
                    <option><%=request.getAttribute("fibonacci")%></option>
                </select>
        <%
            }
        %>

I would greatly appreciate your help, Thank you!

    
asked by Kenny99A 21.10.2018 в 23:49
source

0 answers