Book. jsp is the product list page. Here we are books. Cart. jsp is the shopping cart page.
The simplest shopping cart item quantity can only be 1, can only be added, cannot be deleted.
// Book. jsp, book list
<? XML version = "1.0" encoding = "UTF-8"?>
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="Http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> insert title here </title>
</Head>
<Body>
<%
String [] books = {"jsp", "Java", "Economics", "capital ",
"Mis", "ERP", "CRM", "dataming "};
%>
<Table width = "80%" border = "1">
<Tr>
<TD> books </TD>
<TD> Operations </TD>
</Tr>
<%
For (INT I = 0; I <books. length; I ++ ){
%>
<Tr>
<TD> <% = books [I] %> </TD>
<TD> <a href = "cart. jsp? Book = <% = books [I] %> "> Add to cart </a> </TD>
</Tr>
<%
}
%>
</Table>
<A href = "cart. jsp"> show cart </a>
</Body>
</Html>
// Cart. jsp, shopping cart
<? XML version = "1.0" encoding = "UTF-8"?>
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" Import = "Java. util. *" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="Http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> insert title here </title>
</Head>
<Body>
<%
String bookname = request. getparameter ("book ");
Arraylist cart = (arraylist) Session. getattribute ("cart ");
If (Cart = NULL ){
// First time/
Cart = new arraylist ();
Session. setattribute ("cart", cart );
}
Boolean found = false;
If (bookname! = NULL &&! Bookname. Trim (). Equals ("")){
For (INT I = 0; I <cart. Size (); I ++ ){
String current = (string) cart. Get (I );
If (bookname. Equals (current )){
Found = true;
Break;
}
}
If (! Found ){
Cart. Add (bookname );
}
}
%>
<H1> cart <Table width = "700" border = "1">
<Tr>
<TD> books </TD>
<TD> count </TD>
</Tr>
<%
For (INT I = 0; I <cart. Size (); I ++ ){
String current = (string) cart. Get (I );
Out. println ("<tr> <TD>" + current + "</TD> <TD> 1 </TD> </tr> ");
}
%>
</Table>
<A href = "book. jsp"> continue </a>
</Body>
</Html>