Reputation: 3271
I have a problem creating a list on a Servlet. I have the following code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List lIngredients = new ArrayList<>();
String ingrediente = request.getParameter("Ingredientes");
String action = request.getParameter("action"); //elegimos a qué pantalla pasar en función de la acción que nos llegue de la interfaz
if ("Buscar todas las recetas".equalsIgnoreCase(action)) {
request.setAttribute("AllReceipes", RecetaDao.getAllReceipes());
request.getRequestDispatcher("receipes.jsp").forward(request, response);
}else if ("Buscar por ingredientes".equalsIgnoreCase(action)){
lIngredients.add(ingrediente);
request.setAttribute("AllIngredients", RecetaDao.getSomeReceipes(lIngredients));
request.getRequestDispatcher("perIngredient.jsp").forward(request, response);
}else if ("Agregar ingrediente".equalsIgnoreCase(action)){
lIngredients.add(ingrediente);
}
}
Te problem is that all the time I save the same value. I don´t know if its possible to reset the "ingrediente" and "action" strings that I´ve made and let the user select an other value from the JSP.
Thanks.
This is the JSP:
`<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="./optionServlet" method="POST">
<table border="1">
<form action="demo_form.asp" autocomplete="on">
<th><FONT FACE="Times New Roman" SIZE=3> Ingredientes: </th>
<td><input type="text" name="Ingredientes" value="${Receta.Ingredientes}" list="datalist1" /></td>
<datalist id="datalist1">
<option value="Aceite"><option value="AceiteOliva"> <option value="AceitunasNegras"><option value="Ajo"><option value="Albahaca">
<option value="AlbahacaFresca"><option value="Azucar"><option value="CarnePicada"><option value="Cebolla"><option value="CebollaMorada">
<option value="Croutons"><option value="DienteDeAjo"><option value="Espinaca"><option value="FileteSalmon"><option value="Guisantes">
<option value="Harina"><option value="Huevo"><option value="LaminaParaCanelones"><option value="Lechuga"><option value="Macarrones">
<option value="Mantequilla"><option value="MasaPizza"><option value="Miel"><option value="Mostaza"><option value="Oregano">
<option value="PanRallado"><option value="Patata"><option value="PechugaPollo"><option value="Pepino"><option value="Perejil"><option value="Pimienta">
<option value="PimientoRojo"><option value="QuesoFeta"><option value="QuesoMozzarella"><option value="QuesoParmesano"><option value="QuesoRicota">
<option value="Sal"><option value="SalsaQueso"><option value="Tomate"><option value="TomateTriturado"><option value="Zanahoria">
</datalist>
<button><input type="submit" name="action" value="Buscar todas las recetas" /></button>
<button><input type="submit" name="action" value="Buscar por ingredientes" /></button>
<button><input type="submit" name="action" value="Agregar ingrediente" /></button>
<table>
<tr>
<td> <font color="#74DF00"><b><c:out value="${mensajesOK}"/></b></font> </td>`
This is the correct code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
List lIngredients = (List) session.getAttribute("Ingredientes");
response.setContentType("text/html;charset=UTF-8");
if (lIngredients == null) {
lIngredients = new ArrayList<>();
session.setAttribute("Ingredientes", lIngredients);
}
String ingrediente = request.getParameter("Ingredientes");
String action = request.getParameter("action"); //elegimos a qué pantalla pasar en función de la acción que nos llegue de la interfaz
if ("Buscar todas las recetas".equalsIgnoreCase(action)) {
request.setAttribute("AllReceipes", RecetaDao.getAllReceipes());
request.getRequestDispatcher("receipes.jsp").forward(request, response);
}else if ("Buscar por ingredientes".equalsIgnoreCase(action)){
lIngredients.add(ingrediente);
request.setAttribute("AllIngredients", RecetaDao.getSomeReceipes(lIngredients));
request.getRequestDispatcher("perIngredient.jsp").forward(request, response);
}else if ("Agregar ingrediente".equalsIgnoreCase(action)){
lIngredients.add(ingrediente);
request.getRequestDispatcher("option.jsp").forward(request, response);
ingrediente = request.getParameter("Ingredientes");
action = request.getParameter("action");
}
}
Upvotes: 1
Views: 5410
Reputation: 51711
The issue is that you're creating a new List
at every request.
List lIngredients = new ArrayList<>();
Since, it's a local variable it ceases to exist as soon as that request completes. So, that the previous values are available to you when the user finally decides to search for the recipes, you need to persist the ingredient List
into a session.
HttpSession session = request.getSession();
List lIngredients = (List) session.getAttribute("Ingredientes");
if (lIngredients == null) {
List lIngredients = new ArrayList<>();
session.setAttribute("Ingredientes", lIngredientes);
}
Upvotes: 1