Here's what I figured out so far:
- The session attribute value for the list of users is called ('users"), which is set in the DisplayUsersServlet which returns an ArrayList of users (I tried using DisplayUsersServlet, it works and displays the list in the users.jsp)
- The session attribute value for the update part is called DisplayUserServlet(singular user instead of user
s). This is rendered by user.jsp, which displays a form for the selected user
This tells me that I should pass the user
s session attribute (from the list of users) in the DeleteUsersServlet. This is what I tried:
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.User;
import data.UserDB;
import org.apache.log4j.Logger;
public class DeleteUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// User user = (User) session.getAttribute("users");
String emailAddress = request.getParameter("emailAddress");
UserDB.delete(user);
session.setAttribute("users", user);
String url = "/displayUsers";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
The problem is I can't seem to cast an ArrayList to a User type object and I get the following error:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to business.User
user.DeleteUserServlet.doGet(DeleteUserServlet.jav a:22)
javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
Am I right in assuming that I need to pass the "users" session attribute in the DeleteUsersServlet? Any advice about how to deal with the ClassCastException?
Thank you