Issue
I have one servlet which is called LeggTilHandlekurv (shoppingCart), one jsp, and one Vare (Item) class. The meaning is to add items to a shoppingCart and that is working well, but the question is how can I get the delete function to work? In leggtilhandlekurv.jsp you have the options to type in an item and the item will be displayed with a remove button. And I want the remove button to work. My try so far is in the servlet class marked out, starting from if(vare.equals("remove")).
@WebServlet("/"+LEGGTILHANDLEKURV_URL)
public class LeggTilHandlekurv extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("leggtilhandlekurv.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session= request.getSession();
String varen=request.getParameter("vare");
Vare vare=new Vare();
vare.setVareNavn(varen);
ArrayList<Vare> varer = new ArrayList<Vare>();
varer.add(vare);
if(request.getServletContext().getAttribute("handlekurv")!=null) {
varer=(ArrayList<Vare>) request.getServletContext().getAttribute("handlekurv");
varer.add(vare);
request.getServletContext().setAttribute("handlekurv", varer);
}
else {
request.getServletContext().setAttribute("handlekurv", varer);
}
response.sendRedirect("leggtilhandlekurv.jsp");
/*
if(vare.equals("remove")) {
varer=(ArrayList<Vare>) session.getAttribute("handlekurv");
varer.remove(vare);
session.setAttribute("handlekurv", varer);
request.getRequestDispatcher("leggtilhandlekurv.jsp").forward(request, response);
}
*/
}
}
public class Vare {
private String vareNavn;
public String getVareNavn() {
return vareNavn;
}
public void setVareNavn(String vareNavn) {
this.vareNavn=vareNavn;
}
}
<%@page import="oppgave1.Vare"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Handleliste</title>
</head>
<body>
<%
if(session.getAttribute("pass")==null){
response.sendRedirect("login.jsp");
}
%>
<h1>Min handleliste</h1>
<form action="LeggTilHandlekurv" method="post">
Legg til vare: <br>
<input type="text" name="vare"> <br>
<input type="submit" value="Add item">
</form>
<table>
<th>Varer</th>
<%
ArrayList<Vare> varer = new ArrayList<Vare>();
if(request.getServletContext().getAttribute("handlekurv")!=null){
varer=(ArrayList<Vare>) request.getServletContext().getAttribute("handlekurv");
if(!varer.isEmpty()){
for(Vare vare: varer){
%>
<tr>
<td> <%=vare.getVareNavn() %></td>
<td> <input type="submit" name="remove" value="Remove item"> </td>
</tr>
<%
}
}
}
%>
</table>
</body>
</html>
Solution
You can use hidden input field which will have value of vareNavn
and form tag which will submit your form to servlet and get response back . So , your jsp code will look like below :
<td>
<form action="LeggTilHandlekurv" method="post">
<!-- hidden input-->
<input type="text" hidden name="item" value="<%=vare.getVareNavn() %>">
<!--added name attribute-->
<input type="submit" name="remove" value="Remove item">
</form>
</td>
Then , in you servlet doPost
method you need to get the hidden input value i.e item
and then compare this value with the values store in session
if matches we just need to remove that value from session . So, your doPost
method code will look like below :
//check if request is for remove
if (request.getParameter("remove") != null) {
//get values
String item = request.getParameter("item");
//get data from session
ArrayList<Vare> varer1 = (ArrayList<Vare>) request.getServletContext().getAttribute("handlekurv");
Iterator<Vare> it = varer1.iterator();
//iterate through datas
while (it.hasNext()) {
Vare name = it.next();
//check if values matches
if (name.getVareNavn().equals(item)) {
it.remove(); //remove
}
}
//pass updated value to jsp page
session.setAttribute("handlekurv", varer1);
request.getRequestDispatcher("leggtilhandlekurv.jsp").forward(request, response);
}
Also , give name attribute here <input type="submit" name="add" value="Add item">
as well and put your all code for adding new values inside if(request.getParameter("add") != null){ //put code here }
Answered By - Swati
Answer Checked By - Cary Denson (JavaFixing Admin)