Reputation: 545
first pic here after month reading posts to help me so, kinda excited ! :D I'm currently learning JavaEE in scholl and have a problem to get values from a select box in my JSP file.
Here is what I want to do : get the value from the select box when I click "add to card", put that in a list (in the servlet) et show that list back in my jsp file. I give you the code I have for now (probably kin of ugly, sorry, first days...)
Servlet :
public class CommandeServlet extends HttpServlet {
private List listePlatsCommandes = new ArrayList();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doWork(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doWork(request, response);
}
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CommandeModel beanSessionCommande = (CommandeModel) request.getSession().getAttribute("beanSessionCommande");
if (request.getParameterMap().isEmpty()) {
beanSessionCommande = new CommandeModel();
} else if (request.getParameter("submit") != null) {
listePlatsCommandes.add(request.getParameter("platCommande"));
Commande com = new Commande(request.getParameter("libelle"), listePlatsCommandes);
CommandeManagerSingl.getInstance().ajouter(com);
beanSessionCommande.setCommande(com);
}
System.out.println(listePlatsCommandes);
request.getSession().setAttribute("beanSessionCommande", beanSessionCommande);
request.getRequestDispatcher("/WEB-INF/commande.jsp").forward(request, response);
}
}
Model :
public class CommandeModel {
private List<Plat> plats;
private Commande commande;
public CommandeModel() {
this.plats = new ArrayList<Plat>();
this.commande = new Commande();
}
public List<Plat> getPlats() {
return plats;
}
public void addPlat(Plat plat) {
plats.add(plat);
}
public void setPlats(List<Plat> plats) {
this.plats = plats;
}
public Commande getCommande() {
return commande;
}
public void setCommande(Commande commande) {
this.commande = commande;
}
}
JSP file :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Commande</title>
</head>
<body>
<a href="/PlatServlet">Plats</a> - <a href="/CommandeServlet">Commandes</a>
<h1>Gestion des commandes</h1>
<div>
<form action="CommandeServlet">
<select name="listePlats">
<c:forEach var="plat" items="${beanSessionPlat.listePlats}">
<option name="platCommande" value=" ${plat.libelle} " selected="selected"> ${plat.libelle} </option>
</c:forEach>
</select>
<input type="submit" value="Add to cart" name="submit">
</form>
</div>
<div>
<!-- ICI LA LISTE DE LA COMMANDE -->
<h3>Résumé de la commande :</h3> <br><br>
<table border="1">
<c:forEach var="com" items="${beanSessionCommande.plats}">
<tr>
<td> ${com} </td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
Forgot to say : everything is in Session for now, and i get the select list filled up with a list i get from another JSP file
Thanks a lot for helping :)
Upvotes: 1
Views: 3684
Reputation: 11835
As the only <select>
tag you have has name
equal to listePlats
, it will be submitted under this name.
request.getParameter("listePlats")
will return the value of the selected option
.
By the way, it seems that you mark all options
as selected
. I doubt this is useful: only one (or none) should be marked as such for a non-multi <select>
.
Upvotes: 1