JSP writing small shopping cart system, but defective

Source: Internet
Author: User

Below is my online shopping cart system, but there is a flaw, when you submit items, re-refresh items, you will find the shopping cart inside will automatically add an item, you netizens please teach me little brother

shop.jsp

<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title> online purchase page </title>
<meta http-equiv= "pragma" content= "no-cache";
<meta http-equiv= "Cache-control" content= "No-cache",
<meta http-equiv= "expires" content= "0";
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3";
<meta http-equiv= "description" content= "this Is my page,
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css";
-->
</ Head>

<body>
<form method= "POST" action= "shopcheck.jsp" >
<br>
Please select the type of book you want to add or delete
Add Item:
<select name= "Item" >
<option>java teaching</option>
<option>java Web developing</option>
<option>java web</option>
<option>ssh2 developing</option>
<option>java programming</option>
<option>c program</option>
</select>
<br>
<input type= "Submit" name= "Submit" value= "Add" >
<input type= "Submit" name= "Submit" value= "Remove" >
</form>
</body>

shopcheck.jsp

<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title> purchased Information </title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-

<body>
<%
Request.setcharacterencoding ("GBK");
%>
<jsp:usebean id= "Cart" scope= "session" class= "shop. Shop "/><!--here is the equivalent of creating a shop object--
<jsp:setproperty name= "cart" property= "*"/>

<%
Cart.processrequest (Request);
%>
<br> the books you have purchased are:
<ol>
<%
string[] items = Cart.getitems ();
for (int i = 0; i < items.length; i++) {
%><li><%=items[i]%> <%
}
%>

</ol>
<br>

</body>

Shop.java

Package shop;

Import java.util.*;
Import javax.servlet.http.*;

public class Shop {
Private vector v = new vector ();
Private String submit = null;
Private String item = NULL;

private void AddItem (String name) {
V.addelement (name);
}

private void RemoveItem (String name) {
V.removeelement (name);
}

public void SetItem (String name) {
item = name;
}

public void Setsubmit (String s) {
Submit = S;
}

Public string[] GetItems () {
String[] s = new string[v.size ()];
V.copyinto (s);
return s;
}

public void ProcessRequest (HttpServletRequest request) {
if (submit = = NULL) {//Add items by default
AddItem (item);
Reset ();
}
if (Submit.equals ("add")) {
AddItem (item);

}
if (Submit.equals ("Remove")) {
RemoveItem (item);
}
Reset ();
}

private void Reset () {
Setsubmit (NULL);
SetItem (NULL);

}

Public String Getsubmit () {
return submit;
}

Public String GetItem () {
return item;
}
}

defects, and then refresh the page after submission, very artifact defects came out

JSP writing small shopping cart system, but defective

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.