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