Java 在Servlet上创建ArrayList
在Servlet上创建列表时遇到问题。我有以下代码:Java 在Servlet上创建ArrayList,java,jsp,servlets,arraylist,Java,Jsp,Servlets,Arraylist,在Servlet上创建列表时遇到问题。我有以下代码: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List lIngredients = new ArrayList<>(); String ingrediente = request.ge
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);
}
}
protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException{
List LingElements=new ArrayList();
字符串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(“公共汽车停靠站”。同等信号案例(行动)){
setAttribute(“AllReceipes”,RecetaDao.getAllReceipes());
getRequestDispatcher(“receipes.jsp”).forward(请求,响应);
}否则,如果(“公共汽车乘客”。相等信号案例(行动)){
lIngredients.add(ingrediente);
request.setAttribute(“AllIngredients”,RecetaDao.getSomeReceipes(lIngredients));
getRequestDispatcher(“perIngredient.jsp”).forward(请求,响应);
}else if(“Agregar Ingredite.”等信号案例(行动)){
lIngredients.add(ingrediente);
}
}
问题是我一直保存相同的值。我不知道是否可以重置我创建的“ingrediente”和“action”字符串,并让用户从JSP中选择其他值
谢谢
这是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>`
`
JSP页面
英格瑞登斯:
`
这是正确的代码:
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");
}
}
protectedvoidprocessrequest(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException{
HttpSession session=request.getSession();
List lIngredients=(List)session.getAttribute(“ingredients”);
setContentType(“text/html;charset=UTF-8”);
如果(LingElements==null){
LingElements=新的ArrayList();
session.setAttribute(“ingredients”,lIngredients);
}
字符串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(“公共汽车停靠站”。同等信号案例(行动)){
setAttribute(“AllReceipes”,RecetaDao.getAllReceipes());
getRequestDispatcher(“receipes.jsp”).forward(请求,响应);
}否则,如果(“公共汽车乘客”。相等信号案例(行动)){
lIngredients.add(ingrediente);
request.setAttribute(“AllIngredients”,RecetaDao.getSomeReceipes(lIngredients));
getRequestDispatcher(“perIngredient.jsp”).forward(请求,响应);
}else if(“Agregar Ingredite.”等信号案例(行动)){
lIngredients.add(ingrediente);
getRequestDispatcher(“option.jsp”).forward(请求、响应);
ingrediente=request.getParameter(“Ingredientes”);
action=request.getParameter(“action”);
}
}
问题是您在每次请求时都要创建一个新的列表
List lIngredients = new ArrayList<>();
“我一直保存着相同的值”-你能解释一下你的意思吗?我有一个jsp页面,其中有一个成分列表。我想一直选择一个不同的,而不是一旦我有了一些关于“ingrediente”参数的信息,我就不能选择一个新的,这就是为什么我一直保存着我第一次写的相同的成分,我对外国(西班牙语?)文本有点困惑,但你是不是指while(“Buscar por IngRedients”.equalsIgnoreCase(action))
(no!
)?成分:成分;巴士todas las recetas:寻找所有的receipes;客车:寻找配料;Agregar ingrediente:添加配料。当用户没有按下该按钮时,它应该继续询问。我想清除按钮和入口的信息,也许?(为西班牙语感到抱歉!)你为什么循环?如果操作为“Agregar ingrediente”,循环将变得不确定。请注意,您也需要将列表放入会话中。否则,session.getAttribute()
将始终返回null
。我错过了,现在在我的回答中更新了。
HttpSession session = request.getSession();
List lIngredients = (List) session.getAttribute("Ingredientes");
if (lIngredients == null) {
List lIngredients = new ArrayList<>();
session.setAttribute("Ingredientes", lIngredientes);
}