Practical 6C
Practical 6C
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>