JSP implements the simplest shopping cart

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.