Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The administrator is the owner of the shop who can view the
items in his shop, insert the items and can delete the items. To insert
the items in the shop he has to specify the itemcode, itemname,
number of items he want, and the cost of the item. The HTML page for
inserting the items by the Administrator is:
The psuedo code for inserting the item is:
String code=req.getParameter("itemcode");
String path=req.getParameter("itemspath");
String idis=req.getParameter("itemdesc");
Int totalno=Integer. parseInt(req.getParameter("noofitems"))
String name=req.getParameter ("itemname");
int a =Integer.parseInt(req.getParameter("cost"));
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc:odbc:batch","batch10","batch10");
stmt=con.createStatement();
row=stmt.executeUpdate("insert into itemlist
values('"+code+"','"+path+"','"+name+"',"+totalno+",'"+idis+"',"+a+")");
Con.commit ();
For deleting a particular item he has to specify the itemcode of
the item.
String itemcode=req.getParameter("itemcode");
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("<head><title>loginError</title></head>");
out.println("<body bgcolor=#FFFF89>");
60
out.println("<imgborder=0src=c:/javawebserver2.0/public_html/untitled
.gifwidth=487height=105><fontcolor=#FF00FF><marquee><strong><i
>shop for online shoppers</i></strong></marquee><hr color=red>");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:batch","batch10","bath1
0"); stmt=con.createStatement();
s=con.createStatement();
row=stmt.executeUpdate("delete from itemlist where
itemcode='"+itemcode+"' ");
con.commit();
}
catch(ClassNotFoundException e)
{ System.out.println("CNFE ," +e);
}
If the Itemcode specified is not correct then the exception is raised
saying that the Itemcode is not correct.
catch(SQLException e)
{
out.println("SELECT PROPER ITEMCODE TO
DELETE");
out.println("<ahref=c:/javawebserver2.0/public_html/delitems.html>del
et item</a>");
}
Another exception is raised If the administrator doesn’t specify
the itemcode and clicks the Delete button.
catch(NullPointerException e)
{
out.println("ENTER ITEMCODE TO DELETE");
61
out.println("<ahref=c:/javawebserver2.0/public_html/delitems.html>del
et item</a>");
After clicking the FLOWERS button all the flowers in the shop are
displayed in the Fservlet as follows:
rs = stmt.executeQuery("select * from itemlist where itemcode>'f000'
and itemcode<='f9999'");
out.println("<head><title>Flowers</title></head>");
out.println ("<img
src=c:/javawebserver2.0/public_html/lgon_files/untitled.gif><hr
color=red>");
The table for inserting the items is:
while(rs.next())
{
itemcode[count]=rs.getString(1);
count[count]=rs.getFloat(6);
count=count+1;
out.println("<td bgcolor=#ffff99>");
out.println("<img src='"+rs.getString(2)+"' width=75
height=75>");
if(count==6)
break;
}
out.println("</tr>");
out.println("<tr>");
for(j=0;j<count;j++)
out.println("<tdbgcolor=#ffff99><fontcolor=black><i>Ic:<font
color=red>"+itemcode[j]+"</font></i></font></td>");
out.println("</tr>");
out.println("<tr>");
62
for(j=0;j<count;j++)
out.println("<tdbgcolor=#ffff99><fontcolor=black><i>Rs:"+cost[j]+"</i>
</font></td>");
out.println("</tr>");
out.println("<tr>");
for(j=0;j<count;j++)
out.println("<td bgcolor=#ffff99><font
color=black><i></i></font></td>");
out.println("</tr>");
out.println("<tr>");
for(j=0;j<count;j++)
{
out.println("<formmethod=getaction=http://localhost:8080/servlet
/buyServlet/"+ sessionid +">");
out.println("<td bgcolor=#ffff99><input type= hidden name=itemcode
value='"+itemcode[j]+"'>");
out.println("<divalign=center><inputtype=image
src=c:/javawebserver2.0/public_html/buy1.gif></div></td>");
out.println("</form>");
}
out.println("</tr>");
}
Similarly the code for every item to display is same.i.e;for
ELECTRONICS, GIFT ARTICLES, JEWELLERY etc.
After clicking the buy button the customer has to specify the quantity he
want. After clicking the submit button the quantity is added to his cart.
He is also provided with three options like ADDMORE,VIEWCART
AND BILL. Mere clicking the ADDMORE button he can add more
items to his cart.
The Servlet for receiving the quantity:
out.println("</tr>");
63
out.println("<tr><tdwidth=170><fontsize=2color=black><i>Name:</fo
nt>");
out.println("<fontcolor=red><b><spanstyle=texttransform:uppercase>"
+rs.getString(3)+"</span></b></i></font>");
out.println("</td></tr>");
out.println("<tr><td><font size=2
color=black><i>Quantity:</i></font>");
out.println("<input type=text name=quantity size=3>");
out.println("</td></tr>");
out.println("<tr>");
out.println("<td><div align=center> <input type=submit name=submit
value=AddCart ></div>");
out.println("<td><div align=center> <input type=reset name=reset
value=Reset ></div>");
out.println("</tr>");
out.println("</table>");
If the customer want to buy the same item then the servlet is:
while (rs1.next())
{
if(code.equals(rs1.getString(1))&&sessionid.equals(rs1.getStrin
g(3)))
{
out.println("<font size=3>This item is already added to your
cart<br>");
out.println("<font color= brown size=5><marquee>YOUR ITEM
IS SUCCESSFULLY ADDED TO YOUR
CART</marquee></font>");
out.println("<font color=black size=2>You want to add more item
to your cart click on Addmore</font>");
out.println("<formmethod=getaction=http://localhost:8080/s
ervlet/loginServlet/" + sessionid + ">");
out.println("<div align= center ><input type=submit value=
AddMore></div>");
64
out.println("</form>");
out.println("<font color=black size=2>You want to view
the addcart click on ViewACart</font>");
out.println("<form method =get
action=http://localhost:8080/servlet/ViewACartServlet/" + sessionid +
"> ");
out.println("<div align= center ><input type=submit
value= ViewACart></div>");
out.println("</form>");
out.println("<font color=black size=2>You
want bill</font>");
out.println("<formmethod=getaction=http://localhost:8080/servlet/billSe
rvlet/" + sessionid + ">");
out.println("<div align= center ><input type=submit value=
BILL></div>");
out.println("<form method
=getaction=http://localhost:8080/servlet/modifyServlet/"+ sessionid
+">");
while(rs.next())
{
String code1 =rs.getString(1);
int cont=rs.getInt(2);
String ci=rs.getString(3);
If(sessionid.equals(ci))
{
rs1=s.executeQuery("select * from itemlist");
while(rs1.next())
{
String code=rs1.getString(1);
65
String name=rs1.getString(3);
String desc=rs1.getString(5);
if(code.equals(code1))
{
out.println("<tr><td width=10%><input
type = checkbox name='"+code+"' value = '"+code+"'>");
out.println("<td><div
align=center>'"+code+"'</div><td><div
align=center>'"+name+"'</div><td><div
align=center>"+desc+"'</div><td><div
align=center>"+cont+"</div></tr>");
}
}
}
}
out.println("</table>");
out.println("<font size=2 color=black>To Modify click
submit</font><div align=right><input type=submit
value=submit></div>");
out.println("</form>");
out.println("<form method=get
action=http://localhost:8080/servlet/ATCServlet/" + sessionid + ">");
out.println("<div align=right><input type=submit
value=back></div>");
out.println("</form>");
The customer can modify the quantity he specified. Before that he
has to select the items he has to modify and click the modify
button.
while(enume.hasMoreElements())
{
String name1=(String) enume.nextElement ();
String values []=req.getParameterValues (name1);
66
if(values!=null)
for(int i=0;i<values.length;i++)
{ rs=stmt.executeQuery("select * from temp1 where
custid='"+sessionid+"'"); while(rs.next())
{
String code1=rs.getString(1);
int cont=rs.getInt(2);
if(values[i].equals(code1))
{
rs1=s.executeQuery("selectfromitemlistwhereitemcode='"+values[i]+"'"
); while(rs1.next())
{
String code=rs1.getString (1);
String name=rs1.getString (3);
String desc=rs1.getString(5);
out.println("<td><divalign=center><inputtype=checkbox
name='"+code+"' value='"+code+"'</div>");
out.println("<td><div align=center><input type= text
name='"+code+"' size=4></div>");
String t=code.concat ("/");
Out.println("<input type=hidden name='"+code+"'
value='"+t+"'></tr>");
}
}
}
}
The servlet for modifying the quantity and deleting the item
the customer has selected.
stmt.executeUpdate("update temp1 set count="+a+" where
ic='"+code1+"' and custid='"+sessionid+"'");
67
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:batch","batch10","batch
10"); stmt=con.createStatement();
s=con.createStatement();
rs=stmt.executeQuery("select * from temp1 where
custid='"+sessionid+"'");
while(rs.next())
{
String code1 =rs.getString(1);
int cont=rs.getInt(2);
String name1=rs.getString(3);
rs1=s.executeQuery("select * from item list");
while(rs1.next())
{
String code=rs1.getString(1);
String name=rs1.getString(3);
int toc =rs1.getInt(4);
String desc=rs1.getString(5);
float cost=rs1.getFloat(6);
if(code.equals(code1))
{
x++;
tco=cont * cost;
fco=fco+tco;
68
y=y+cont;
out.println("<tr><td><div
align=center>"+x+"</div><td><div
align=center>'"+code+"'</div><td><div
align=center>'"+name+"'</div><td><div
align=center>"+desc+"'</div><td><div
align=center>"+cont+"</div><td><div
align=center>"+cost+"</div><td><div
align=center>"+tco+"</div></tr>");
}
}
}
69
out.println("<form
action=http://localhost:8080/servlet/checkOutservlet/" + sessionid +
">");
After billing when the customer clicks the Checkout button the
servlet Checkout is invoked where the customer credit card
number is taken and is checked with the Bank database.
The Check out servlet where the customer enters his Credit
card number.
70
if(sessionid!=null)
{
sessionid=sessionid.substring(1,a);
out.println("<form method=get
action=http://localhost:8080/servlet/check/" + sessionid + ">");
out.println("<div align=center><table border=0
width=53%>");
out.println("<tr><th width=50%>Credit Card id:<td
width=25%><input type =password name =t1></tr>");
out.println("</table></div>");
out.println("<div align=right><input type=submit
name=submit value=submit></div></form>");
}
}
After giving the credit card number, when the customer clicks the
Check out button the servlet invoked is Check servlet where the
validity of the credit card is checked.
public class check extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse
res)
throws ServletException,IOException
{
Connection con=null;
Statement stmt=null;
Statement s=null,s1=null;
Result Set rs=null;
ResultSet rs1=null,rs2=null;
int count=0,r,x=0,y=0,flag=0;
String cri =req.getParameter("t1");
71
String sessionid=req.getPathInfo();
int a=sessionid.length();
float tco=0,fco=0;
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<head><title>buy item</title></head>");
out.println("<BODY bgColor=#ffff89>");
out.println("<IMG border=0 height=105
src=c:/javawebserver2.0/public_html/lgon_files/untitled.gif
width=487>");
out.println("<FONT
color=#ff00ff><MARQUEE><STRONG><I>shop for online
shoppers</I></STRONG></MARQUEE></FONT>");
out.println("<hr color=red>");
out.println("<div align=center><font size=5 color=green>BILL
REPORT</font></div>");
if(sessionid!=null)
{
sessionid=sessionid.substring(1,a);
out.println("<div align=center><font size=2
color=black>CUSTOMER ID:</font><font size =3
color=red><b><i><u>'"+sessionid+"'</b></i></u></div>");
To check the validity of the credit card number and to know the
amount in his credit, the servlet code is:
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:batch","
batch10","batch10");
stmt=con.createStatement();
72
s=con.createStatement();
s1=con.createStatement();
rs=stmt.executeQuery("select*from total where
userid='"+sessionid+"' ");
rs.next();
float aa=rs.getFloat(2);
rs1=s.executeQuery("select*from bankdb where
creditno='"+cri+"'");
rs1.next();
double bb=rs1.getDouble(3);
If the amount of the customer is allowable then the bank
database is updated by taking the amount from his bill and
displaying a message stating, “your shopping is successful”.
The product will be delivered to the delivery date given by the
shopper.
if(aa<bb)
{
bb=bb-aa;
r=s1.executeUpdate("update bankdb set
amount="+bb+" where creditno='"+cri+"'");
con.commit();
out.println("<div align=center><font size=5
color=blue>YOUR SHOPPING IS SUCCESSFULLY
COMPLETED</font></div>");
out.println("<div align=center><font size=7
color=green>YOUR ITEMS WILL BE DELIVERED AS EARLY AS
POSSIBLE</font></div>");
out.println("<a
href=http://localhost:8080/servlet/signoutServlet/" + sessionid +
">signout</a>");
r=s1.executeUpdate("delete from temp1");
con.commit();
r=s1.executeUpdate("delete from total");
73
con.commit();
}else
If the credit of the customer is not allowable then the exception is
raised saying that YOU HAVE NOT ENOUGH MONEY TO
PURHASE THE ITEMS and to check your amount again.
{
out.println("<div align=center><font size=5
color=blue> YOU HAVE NOT ENOUGH MONEY</FONT></div>");
out.println("<div align=center><font size=10
color=blue>PLEASE CHECK ONCE AGAIN YOUR
AMOUNT</font></div>");
out.println("<a
href=http://localhost:8080/servlet/checkOutservlet/" + sessionid +
"><div align=left><font size=4 color=black>try
again</font></div></a>");
out.println("<a
href=http://localhost:8080/servlet/signOutServlet/" + sessionid + "><div
align=right><font size=4 color=black>Sign out</font></div></a>");
}
}
catch(ClassNotFoundException e)
{
System.out.println("CNFE ," +e);
}
catch(SQLException e)
{ flag=1;
}
finally
{
try
{
74
if(con!=null)
con.close();
}catch(SQLException ignored)
{ }
}
}
If the customer’s credit card number is not valid i.e., the credit
card number is not in the Bank database, then a exception is
raised stating “your credit card number is not valid”
if(flag==1)
{
out.println("<font colr = blue size =6>Enter the Valid
credit card number</font></br>");
out.println("<div align =right><a
href=http://localhost:8080/servlet/checkOutservlet/" + sessionid +
"><i>click here to go back</i></a></div>");
}
}
}
75