Great Deals

Session Demo View Cart Using Jsp and Servlet

10:07 suresh chaudhari 0 Comments

Add Item Then Show LIst
package com.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webapp.model.Product;

/**
 * Servlet implementation class ListController
 */
@WebServlet("/ListController")
public class ListController extends HttpServlet {
private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListController() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products=new ArrayList<Product>();
Product product1=new Product("Colgate",25, 2, "ToothPaste");
Product product2=new Product("Miswaak",50, 2, "Shiny teeth fights with germs");
products.add(product1);
products.add(product2);
request.setAttribute("deepti", products);
RequestDispatcher rd=request.getRequestDispatcher("/list.jsp");

rd.forward(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}
Then Add to cart


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> -->
<title>Insert title here</title>
<!-- <link href="/js/bootstrap.min.css" rel="stylesheet"> -->

   <!-- <script src="/js/bootstrap.min.js"></script> -->
</head>
<body>


<c:out value="${product.price}"/>



<c:forEach  items="${deepti}" var="product"  >   
   <div class = "col-sm-6 col-md-3">
      <div class = "thumbnail">
         <img src = "/bootstrap/images/kittens.jpg" alt = "Generic placeholder thumbnail">
      </div>
      
      <div class = "caption">
         <h3><c:out value="${product.price}"/></h3>
         <p><c:out value="${product.productDesc}"/></p>
         <p><c:out value="${product.productName}"/></p>
         <p><a href="/WebProject/Cart?add=<c:out value='${product.productName}'/>">Add to Cart</a>
          <p><a href="/WebProject/Cart?viewCart=1">View Cart</a>
            <a href = "#" class = "btn btn-primary" role = "button">
               Button
            </a> 
            
            <a href = "#" class = "btn btn-default" role = "button">
               Button
            </a>
         </p>
         
      </div>
   </div>
   </c:forEach>
   
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"/>
</body>
</html>
Then add to Session
package com.webapp.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class CartController
 */
public class CartController extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartController() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
    /**
     * Will add the add to cart item using session
     */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("add")!=null)
{
HttpSession sessionObj=request.getSession();
sessionObj.setAttribute("cartItems",request.getParameter("add"));
}
if(request.getParameter("viewCart")!=null)
{
HttpSession sessionObj=request.getSession();
String cartItem=(String)sessionObj.getAttribute("cartItems");
request.setAttribute("cartItems",cartItem);
RequestDispatcher rd=request.getRequestDispatcher("/showCart.jsp");
rd.forward(request, response);
}
}

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

}
THen show your item added to cart

ShowCart.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String cartItem=(String)session.getAttribute("cartItems");

%>
</body>
<div id=""><%=cartItem %></div>
</html>

0 comments:

Advertising