Send variable from Scriptlet to Servlet

0

Good evening people from Stack Overflown, I write pq I have a problem and I would like to see if you could help me

A teacher sent us an exercise where you should create a shopping cart and by pressing the submit button you should show the results in a Servlet, my problem is that sending the result to the servlet does not show me the amount or the Total product that I have chosen, only shows me the initial value that you assign to the variable

I attach the Scriptlet and Servlet

SCRIPTLET

<%-- 
    Document   : index2
    Created on : 12-04-2018, 10:21:25
    Author     : alumnos09
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Kwik E Mart</title>

    </head>

    <body>

        <header>

            <h3>Bienvenidos a Kwik E Mart</h3>

        </header>

        <main>

            <form action="Kwik_E_Mart" method="post">

                Escoja sus productos a llevar:

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Batidos"> Batidos - 1000 $
                <input type="number" name="valor1" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Pan"> Pan - 200 $
                <input type="number" name="valor2" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Huevos"> Huevos - 500 $
                <input type="number" name="valor3" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Leche"> Leche - 3000 $
                <input type="number" name="valor4" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Helado"> Helado - 2500 $
                <input type="number" name="valor5" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Gomitas"> Gomitas - 1200 $
                <input type="number" name="valor6" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Harina"> Harina - 550 $
                <input type="number" name="valor7" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Azucar"> Azucar -475 $
                <input type="number" name="valor8" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Sal"> Sal - 300 $
                <input type="number" name="valor9" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Rosquillas"> Rosquillas - 650 $
                <input type="number" name="valor10" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Perro Caliente"> Perro Caliente - 870 $
                <input type="number" name="valor11" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Papas Fritas"> Papas Fritas - 1000 $
                <input type="number" name="valor12" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Tabaco"> Tabaco - 630 $
                <input type="number" name="valor13" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Agua"> Agua - 990 $
                <input type="number" name="valor14" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                <input type="checkbox" name="producto" value="Refresco"> Refresco - 1490 $
                <input type="number" name="valor15" placeholder="Cantidad" min="0" value="0" required="true">

                <br/>
                <br/>

                Escoja su metodo de pago:

                <input type="radio" name="pago" value="Efectivo" checked> Efectivo
                <input type="radio" name="pago" value="Tarjeta de Debito o Credito"> Tarjeta de Debito o Credito
                <input type="radio" name="pago" value="APU"> Tarjeta APU´s

                <br/>
                <br/>

                <% session.setAttribute("precio",Integer.valueOf(cantidad_total)); %>

                <input type="submit" value="Terminar Compra">

            </form>

            <% int cantidad_total = 0; int valor = 0; int cantidad_llevar = 0; int productos_llevar = 0; %>

            <% String productos[] = request.getParameterValues("producto"); %>

            <% if (productos != null) { %>

                <% for (int i = 0; i < productos.length; i++) { %>

                    <% if (productos[i].equalsIgnoreCase("Batidos")) { %>

                        <% if (request.getParameter("valor1") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor1")); %>

                            <% valor = 1000 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Pan")) { %>

                        <% if (request.getParameter("valor2") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor2")); %>

                            <% valor = 200 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Huevos")) { %>

                        <% if (request.getParameter("valor3") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor3")); %>

                            <% valor = 500 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Leche")) { %>

                        <% if (request.getParameter("valor4") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor4")); %>

                            <% valor = 3000 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Helado")) { %>

                        <% if (request.getParameter("valor5") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor5")); %>

                            <% valor = 2500 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Gomitas")) { %>

                        <% if (request.getParameter("valor6") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor6")); %>

                            <% valor = 1200 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Harina")) { %>

                        <% if (request.getParameter("valor7") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor7")); %>

                            <% valor = 550 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Azucar")) { %>

                        <% if (request.getParameter("valor8") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor8")); %>

                            <% valor = 475 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Sal")) { %>

                        <% if (request.getParameter("valor9") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor9")); %>

                            <% valor = 300 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Rosquillas")) { %>

                        <% if (request.getParameter("valor10") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor10")); %>

                            <% valor = 650 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Perro Caliente")) { %>

                        <% if (request.getParameter("valor11") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor11")); %>

                            <% valor = 870 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Papas Fritas")) { %>

                       <% if (request.getParameter("valor12") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor12")); %>

                            <% valor = 1000 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Tabaco")) { %>

                        <% if (request.getParameter("valor13") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor13")); %>

                            <% valor = 630 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Agua")) { %>

                        <% if (request.getParameter("valor14") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor14")); %>

                            <% valor = 990 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% } else if (productos[i].equalsIgnoreCase("Refresco")) { %>

                        <% if (request.getParameter("valor15") != null) { %>

                            <% cantidad_llevar = Integer.parseInt(request.getParameter("valor15")); %>

                            <% valor = 1490 * cantidad_llevar; %>

                            <% productos_llevar = productos_llevar + cantidad_llevar; %>

                            <% cantidad_total = cantidad_total + valor; %>

                        <% }; %>

                    <% }; %>

                <% }; %>

            <% }; %>

        </main>

    </body>

</html>

SERVLET

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import static org.apache.jasper.runtime.JspRuntimeLibrary.include;

/**
 *
 * @author alumnos09
 */
@WebServlet(urlPatterns = {"/Kwik_E_Mart"})
public class Kwik_E_Mart extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Kwik_E_Mart</title>");            
            out.println("</head>");
            out.println("<body>");

            HttpSession session=request.getSession();
            String precio = (String) (session.getAttribute("precio"));

            if (request.getParameter("pago").equalsIgnoreCase("efectivo")) {

                out.println("El metodo de pago seleccionado fue: Efectivo");
                out.println("No tiene descuento por este metodo de pago");

                response.getWriter().print("<p> La variable vale " + precio + "</p>");

            } else if (request.getParameter("pago").equalsIgnoreCase("tarjeta de debito o credito")) {

                out.println("El metodo de pago seleccionado fue: Tarjeta de Debito o Credito");
                out.println("No tiene descuento por este metodo de pago");

            } else if (request.getParameter("pago").equalsIgnoreCase("apu")) {

                out.println("El metodo de pago seleccionado fue: Tarjeta APU´s");
                out.println("Felicidades, podra obtener un descutno por una compra mayor a 5 productos.");

            };

            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

The value I want to take to the Servlet is the one that says total_amount

The exercise asks that the formulas be made in Scriptlet and the results be shown in Servlet, I am a newbie in this topic.

I hope you can help me, thanks for your attention.

    
asked by Andrew Portillo 13.04.2018 в 06:06
source

1 answer

1

Add a input to the form of type hidden , and assign the value there. In this way, when you submit the form to the servlet, you can access the value through the request.

So, in your html and after calculating the value of cantidad_total :

<input type="hidden" value="<%=cantidad_total%>" name="sumaCantidadTotal"/>

You must add this within <form> , therefore you should also add the part of the total_quantum calculation within this.

Done that, and already in your servlet, in doPost :

int cantidadTotal = Integer.parseInt(request.getParameter("sumaCantidadTotal"));
    
answered by 13.04.2018 в 08:48