Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: i33naxo en 12 de Enero 2017, 14:55
-
Hola a todos, espero que puedan ayudarme.
Tengo un archivo index.jsp que recoge unos datos (un numero y un múltiplo)
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bonus</title>
</head>
<body>
<h1>Bonus</h1>
<form method="get" action="/WebApplication4/NewServlet">
<table>
<tr>
<th>Numero</th>
<th>Multiplo</th>
</tr>
<c:forEach var="i" begin="0" end="2" step="1">
<tr>
<td><input type="text" value="" name="numero"/></td>
<td><input type="text" value="" name="multiplo"/></td>
</tr>
</c:forEach>
</table>
<button type="submit">Enviar</button>
<button type="reset">Reiniciar</button>
</form>
</body>
</html>
A continuación, paso estos números a un Servlet (NewServlet.java)
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
public double getBonus(String multiplo) {
return Double.parseDouble(multiplo) * 100.0;
}
/**
* 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");
int k = 0;
Enumeration Data = request.getParameterNames();
Map<String, String[]> Element = new HashMap<String, String[]>();
ArrayList<String> myList = new ArrayList();
while (Data.hasMoreElements()) {
String Param = (String)Data.nextElement();
String[] ValeursParam = request.getParameterValues(Param);
myList.add(Param);
Element.put(Param, request.getParameterValues(Param));
k++;
}
List<HashMap<String, String>> List = new ArrayList<HashMap<String, String>>();
int i = 0;
for(i=0; i<Element.get(myList.get(0)).length; i++) {
HashMap<String, String> ElementS = new HashMap<String, String>();
for(int j=0; j<k; j++) {
ElementS.put(myList.get(j), Element.get(myList.get(j))[i].toString());
}
List.add(ElementS);
}
Beans servlet = new Beans();
servlet.setListBeans(List);
request.setAttribute("Beans", servlet);
request.getRequestDispatcher("bonus.jsp").forward(request, response);
}
// <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>
}
Los datos del array los paso a un Beans
package servlet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Beans {
private List<HashMap<String, String>> ListBeans;
public List<HashMap<String, String>> getListBeans() {
return this.ListBeans;
}
public void setListBeans(List<HashMap<String, String>> ListBeans) {
this.ListBeans = ListBeans;
}
}
Y finalmente otro archivo .jsp los muestra
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bonus</title>
</head>
<body>
<h1>Bonus</h1>
<c:forEach items="${Beans.listBeans}" var="ElementS" varStatus="status">
<div class="ElementS">
<div class="numeroElementS">
Numero: <c:out value="${ !empty ElementS['numero'] ? ElementS['numero'] : 'Vacio' }" />
</div>
<div class="bonusElementS">
Bonus: <c:out value="${ !empty ElementS['bonus'] ? ElementS['bonus'] : 'Vacio' }" />
</div>
</div>
</c:forEach>
</body>
</html>
Pues bien, quiero que en el archivo Servlet, los valores "múltiplo" sean editados (necesito multiplicarlos por otro valor) y sea este resultado (llamado bonus) y no el "múltiplo", lo que muestre el archivo bonus.jsp
Espero que hayan entendido el problema.
Gracias por su ayuda.
-
Buenas, no sé si te puede servir, pero cuando quieres hacer una modificación al mismo tiempo que haces un recorrido de una colección lo que debes usar es un iterador. Los conceptos básicos sobre iteradores están explicados en el curso http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=68&Itemid=188
En este otro curso hay algunos ejemplos más avanzados http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=58&Itemid=180
Salu2