Hola, soy nuevo aquí. Antes de nada comentar que estoy empezando con java y me cuesta bastante.
Os expongo el problema:
Debo hacer una aplicación web que recoja unos datos (.jsp) los pase a un servlet, este a un beans para encapsular los objetos en uno solo y la salida de los datos la muestre otra página .jsp
Index.jsp
<%@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 Calculation</title>
</head>
<body>
<h1>Bonus Calculation</h1>
<form method="get" action="/WebApplication4/NewServlet">
<table>
<tr>
<th>Numero SSN</th>
<th>Multiple</th>
</tr>
<c:forEach var="i" begin="0" end="2" step="1">
<tr>
<td><input type="text" value="" name="nombre"/></td>
<td><input type="text" value="" name="multiplier"/></td>
</tr>
</c:forEach>
</table>
<button type="submit">Envoyer</button>
<button type="reset">Réinitialiser</button>
</form>
</body>
</html>
NewServlet.java
(Aquí es donde tengo el problema y no se como tomar los datos, porque he probado de diferentes formas)
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
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 multiplier) {
return Double.parseDouble(multiplier) * 100.0;
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
int counter = 0;
Enumeration Data = request.getParameterNames();
Map<String, String[]> calcul = new HashMap<String, String[]>();
ArrayList<String> myList = new ArrayList();
while (Data.hasMoreElements()) {
String Param = (String)Data.nextElement();
myList.add(Param);
calcul.put(Param, request.getParameterValues(Param));
counter++;
}
List<Map<String, String>> List = new ArrayList<Map<String, String>>();
for(int i=0; i<calcul.get(myList.get(0)).length; i++) {
Map<String, String> calcul2 = new HashMap<String, String>();
for(int j=0; j<counter; j++) {
calcul2.put(myList.get(j), calcul.get(myList.get(j))[i].toString());
}
List.add(calcul2);
}
/*AQUI ES DONDE DEBO EXTRAER LOS DATOS DEL ARRAY PARA PASARLOS AL BEANS*/
Beans servlet = new Beans();
servlet.setNombre(nombre);
servlet.setBonus(bonus);
request.setAttribute("Beans", servlet);
request.getRequestDispatcher("bonus.jsp").forward(request, response);
}
}
Beans.java
package servlet;
public class Beans {
private String nombre, bonus;
public String getNombre(){
return this.nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getBonus(){
return this.bonus;
}
public void setBonus(String bonus) {
this.bonus = bonus;
}
}
Bonus.jsp
<%@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>JSP Page</title>
</head>
<body>
<h1>Bonus Calculation:</h1>
<c:forEach items="${List}" var="calcul">
<div class="calcul">
<div class="nombreCalcul">
<c:out value="${calcul['nombre']}" />
</div>
<div class="bonusCalcul">
<c:out value="${calcul['bonus']}" />
</div>
</div>
</c:forEach>
</body>
</html>
Espero que puedan ayudarme, gracias.
Editado