0% found this document useful (0 votes)
33 views

Practical 6C

1. A web application named ShoppingCart is created along with a Stateful Session Bean also called ShoppingCart. 2. An index.jsp web client is created to interact with the ShoppingCart bean. 3. A database table called 'cart' is created to store username, itemname pairs for the user's shopping cart contents.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Practical 6C

1. A web application named ShoppingCart is created along with a Stateful Session Bean also called ShoppingCart. 2. An index.jsp web client is created to interact with the ShoppingCart bean. 3. A database table called 'cart' is created to store username, itemname pairs for the user's shopping cart contents.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

The following actions need to be performed:

1. Create a Web application named ShoppingCart


2. Create a Stateful Session Bean ShoppingCart
3. Create a web client using JSP named index.jsp
4. Create the database table
-->create table cart(username varchar(30),itemname varchar(30))

ShoppingCart.java(stateful session Bean)

package ejb;
import java.sql.*;
import java.util.*;
import javax.ejb.Remove;
import javax.ejb.Stateful;

@Stateful
public class ShoppingCart {
List<String> contents;
String custname;
Connection con=null;
ResultSet rs;
Statement st=null;
public void initialize(String person)
{
if(person !=null)
{
custname=person;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/tyit","root","cbs123");
}
catch(Exception e)
{
System.out.println("Sorry!!!Failed to connect to the database");
}
}
contents=new ArrayList<String>();
}
public void addbook(String title)
{
try
{
st=con.createStatement();
st.executeUpdate("insert into cart values('"+custname+"','"+title+"')");
}
catch(Exception e)
{
System.out.println("Sorry!!!Failed to insert values to the database");
}
}
public void removebook(String title)
{
try
{
st=con.createStatement();
st.executeUpdate("delete from cart where
username='"+custname+"'anditemname='"+title+"'");
}
catch(Exception e)
{
System.out.println("Sorry!!!Failed to delete values from the database");
}
}
public List<String>getContents()
{
try
{
st=con.createStatement();
rs=st.executeQuery("select * from cart where username='"+custname+"'");
while(rs.next()){
contents.add(rs.getString("itemname"));
System.out.println(rs.getString("itemname"));}
}
catch(Exception e)
{
System.out.println("Sorry!!!Failed to select values from the database");
}
return contents;
}
@Remove
public void remove()
{
contents=null;
}
}

index.jsp

<%@page import="ejb.ShoppingCart"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private static ShoppingCart cart;
public void jspInit()
{
try
{
InitialContext ic=new InitialContext();
cart=(ShoppingCart)ic.lookup("java:global/ShoppingCart/ShoppingCart");
}
catch(Exception e)
{
System.out.println("Could not create cart bean");
}
cart.initialize("Guest");
}
%>
<%
if(request.getParameter("txtcustname")!=null)
cart.initialize(request.getParameter("txtcustname"));
else
cart.initialize("Guest");
%>
<%
if(request.getParameter("btnremove")!=null)
{
String books[]=request.getParameterValues("chkbook");
if(books!=null)
{
for(int i=0;i<books.length;i++)
cart.removebook(books[i]);
}
}
if(request.getParameter("btnadd")!=null)
{
String books[]=request.getParameterValues("chkbook");
if(books!=null)
{
for(int i=0;i<books.length;i++)
cart.addbook(books[i]);
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Shopping Cart</h1>
<form name="frmbooks" method="post">
Customer:
<input type="text" name="txtcustname" value="<%=request.getParameter("txtcustname")
%>"/><br/>
Book Titles:<br/>
<input type="checkbox" name="chkbook" value="C Programming"/>C Programming<br/>
<input type="checkbox" name="chkbook" value="C++ Programming"/>C++ Programming<br/>
<input type="checkbox" name="chkbook" value="Java Programming"/>Java
Programming<br/>
<input type="checkbox" name="chkbook" value="HTML Programming"/>HTML
Programming<br/>
<input type="submit" value="Add" name="btnadd"/>
<input type="submit" value="remove" name="btnremove"/>
<input type="submit" value="show" name="btnshow"/>
<br/>
<table border="1">
<tr>
<td>Basket</td>
</tr>
<%
if(request.getParameter("btnshow")!=null)
{
List <String> booklist=cart.getContents();
Iterator iterator=booklist.iterator();
while(iterator.hasNext())
{
String title=(String)iterator.next();
%>
<tr>
<td><%=title%></td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>

You might also like