📄 cart.java
字号:
package server.servlet;
import java.io.*;
import java.util.*;
//import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import server.servlet.OperateCookie.OperateCookie;
import javax.servlet.http.Cookie;
import server.database.*;
import java.sql.*;
/**
* @version 1.0
* @author
*/
public class Cart extends HttpServlet implements Servlet {
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
/* private Vector splitString(String s,char c,boolean trim){
Vector v = new Vector();
String x = new String();
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) == c){
if(trim){
x = x.trim();
}
v.add(x);
x = new String();
}
else{
x += s.charAt(i);
}
}
if(trim){
x = x.trim();
}
v.add(x);
return v;
};*/
private String splitString(String s,char c, boolean trim){
String x = new String();
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) == c){
x = new String();
}
else{
x +=s.charAt(i);
}
}
if(trim){
x = x.trim();
}
return x;
};
//private Vector items;
private String item;
private Cookie Cart_cookie = null;
//
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setHeader("Cache-Control","no-cache");
resp.setContentType("text/html");
//Cart_cookie.setValue(items_id);
//items = items_id.split("//");
String method = req.getParameter("method");
if(method.equals("add")){
//item = this.splitString(items_id,' ',false);
String item = req.getParameter("subitem");
String userID = req.getParameter("userID");
//存入数据库
String sql_check = "select id from Cart where userID='"+userID+"'";
String sql = "INSERT INTO Cart VALUES('"+item+"','"+userID+"')";
try{
Operation op=new Operation();
ResultSet tmp = op.select(sql_check);
if(tmp.next()){
boolean wExist = false;
tmp.previous();
while(tmp.next()){
if(item.equals(tmp.getString("id"))){
wExist = true;
break;
}
else{
wExist = false;
}
}
if(wExist){
System.out.println("1");
}
else{
System.out.println("2");
op.IDU(sql);
}
}
else{
System.out.println("3");
op.IDU(sql);
}
}
catch(ClassNotFoundException e){}
catch(SQLException e){}
catch(InstantiationException e){}
catch(IllegalAccessException e){}
//resp.addCookie(Cart_cookie);
}
else if(method.equals("sub")){
String subitem = req.getParameter("subitem");
String userID = req.getParameter("userID");
String sql = "DELETE FROM Cart WHERE (id='"+subitem+"' AND userID='"+userID+"')";
try{
Operation op=new Operation();
op.IDU(sql);
System.out.println("4");
}
catch(ClassNotFoundException e){}
catch(SQLException e){}
catch(InstantiationException e){}
catch(IllegalAccessException e){}
}
else if(method.equals("subAll")){
String userID = req.getParameter("userID");
String sql = "delete from Cart where userID='"+userID+"'";
try{
Operation op=new Operation();
op.IDU(sql);
System.out.println("5");
}
catch(ClassNotFoundException e){}
catch(SQLException e){}
catch(InstantiationException e){}
catch(IllegalAccessException e){}
}
}
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -