GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price. Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session – or just their keys – in the listener or an associated class).

GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price.  Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session  –  or just their keys  –  in the listener or an associated class).

GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price. Create a listener that notifies

Jump to GTU WTAD Practical 22

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu_21\

<html>
	<head>
		<title>GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price.  Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session  –  or just their keys  –  in the listener or an associated class).</title>
	</head>
	<body>
	
		<br><h2>GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price.  Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session  –  or just their keys  –  in the listener or an associated class).</h2><br><br><br>
		<center><h2>Select Product wish to purchase. </h2></center>
		<form method="GET" action="product">
			Mouse : <input type="checkbox" name="mouse" value="100"> </br>
			Keyboard : <input type="checkbox" name="keyboard" value="200"> </br>
			Monitor : <input type="checkbox" name="monitor" value="3000"> </br>
			<input type="submit" value="Add to Cart">
			
		</form>
	
		<br><br><br><br><br><br><br><br><br><br><br><br>
		
	</body>
</html>

 

File Name : product.java

File Location : Tomcat_Apache\webapps\gtu_21\WEB-INF\classes

/*
		GTU WTAD Practical 21 :   Create a Java class called Product with the following properties: name, description, price.  Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session  –  or just their keys  –  in the listener or an associated class).
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class product extends HttpServlet
{

	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	{
		int total=0;
		
		HttpSession session = req.getSession();
		PrintWriter out = res.getWriter();
		//session.removeAttribute("total");
		try
		{	
			res.setContentType("text/html");
			out.println("<html>"+"<body>");
			out.println("<center><h2>Items is the shopping cart is</h2></center><br>");
			out.println("<table><tr><th>Product Name</th><th>Price</th></tr>");
			
			Enumeration parameters=req.getParameterNames();
			while(parameters.hasMoreElements())
			{
				String productname=(String)parameters.nextElement();
				int price=Integer.parseInt(req.getParameter(productname));
				
				session.setAttribute(productname, price);
			}

			Enumeration sessionAllAttributes =session.getAttributeNames();
			while(sessionAllAttributes.hasMoreElements())
			{
				try
				{
					String sessionAttributeName=(String)sessionAllAttributes.nextElement();
					int sessionAttributeValue=(int)session.getAttribute(sessionAttributeName);
					total=total+sessionAttributeValue;
					out.println("<tr><td>" + sessionAttributeName + "</td><td>" + sessionAttributeValue + "</td></tr>") ;
				}catch(Exception e)
				{
					out.println(e.getMessage());
				}
				
			}
			
			out.println("<tr><th>Total</th><th>" + total + "</th></tr>") ;
			out.println("</table>");
			out.println("<br><br><a href=\"index.jsp\"> Back to Product List </a>");
			out.println("</body></html>");
		}
		catch (Exception e)
		{
		
		}

	}
}

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu_21\WEB-INF

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 21  </display-name>
  <description>  Create a Java class called Product with the following properties: name, description, price.  Create a listener that notifies (through System.out) whenever a user adds a product to a shopping cart (i.e. adds an object to the session object) or removes it again. Hint: check out the class HttpSessionAttributeListener. Make it print the name and price of the object (hint: access the session through the HttpBindingEvent object). Also, let the listener print the total price of all objects saved in the session so far (one way to accomplish this could be to keep a collection of all objects saved to the session  -  or just their keys  -  in the listener or an associated class). </description>
  
  <servlet>
	<servlet-name>product</servlet-name>
	<servlet-class>product</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>product</servlet-name>
	<url-pattern>/product</url-pattern>
  </servlet-mapping>
  
  
   
</web-app>

 

Output : 1

GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price. Create a listener that notifies

Output : 2

GTU WTAD Practical 21 : Create a Java class called Product with the following properties: name, description, price. Create a listener that notifies

Leave a Reply