会话getAttribute ClassCastException

时间:2018-06-19 20:39:21

标签: java session exception servlets casting

晚安社区, 这是我的第一篇文章,所以首先,我要感谢大家的辛勤工作。

我在Servlet中的会话出现了问题(也许答案也很容易,我现在无法“看到”它。)

Servlet 1。

public void init() {
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    getCustomerIdFromSessionRequest(request);

    out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet</title>");
    out.println("</head>");
    out.println("<body>");

    String action = request.getParameter("action");
    if (action != null && action.equals("addArticle")) {
        String articleId = request.getParameter("ArticleId");
        if (articleId != null) {
            Article article = shop.getArticle(Integer.parseInt(articleId));
            printAddedArticle(article);
            cart.addCartItem(article);
        }
    }
    printCatalog();
}

private Integer getCustomerIdFromSessionRequest(HttpServletRequest request) {
    HttpSession session = request.getSession();
    session.setAttribute("de.leuphana.webshop.customerId", session.getId());
    session.setAttribute("de.leuphana.webshop.cart", cart.getCartItems());          

    return customerId;
}

Servlet 2

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    HttpSession session = request.getSession(false);
    String customerId=(String)session.getAttribute("de.leuphana.webshop.customerId");
    Cart cart=(Cart)session.getAttribute("de.leuphana.webshop.cart");

    System.out.println(customerId);
    System.out.println(cart.getNumberOfArticles());
    System.out.println(cart.getCartItems());

    out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cart</title>");
    out.println("</head>");
    out.println("<body>");
    printCart(cart);
    out.println("</br><a href=\"showCatalog\"> Show Catalog </a>");
    out.println("</body>");
    out.println("</html>");
}

ATM我收到消息: 消息java.util.HashMap $ Values无法转换为de.leuphana.shop.structure.Cart

现在我无法确切地找出问题所在。

在此我可以阅读一些有关此主题的信息,提示,评论或链接,谢谢。

最好的问候

e:消息用于servlet 2行     购物车cart =(Cart)session.getAttribute(“ de.leuphana.webshop.cart”);

0 个答案:

没有答案