📄 shoppingservlet.java
字号:
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import shopping.Film;
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession();
if (session == null) {
res.sendRedirect("http://localhost:8080/big-bird/error.html");
}
Vector buylist=
(Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
Film afilm = getFilm(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector(); //first order
buylist.addElement(afilm);
} else { // not first buy
for (int i=0; i< buylist.size(); i++) {
Film fm = (Film) buylist.elementAt(i);
if (fm.getStar().equals(afilm.getStar())) {
fm.setQuantity(fm.getQuantity()+afilm.getQuantity());
buylist.setElementAt(fm,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(afilm);
}
}
session.putValue("shopping.shoppingcart", buylist);
String page;
page =(String) session.getAttribute("shoppingname");
String url="";
if(page.equals("EShop1"))
{
url="/EShop.jsp";
}
if(page.equals("EShop2"))
{
url="/EShop2.jsp";
}
if(page.equals("EShop3"))
{
url="/EShop3.jsp";
}
if(page.equals("EShop4"))
{
url="/EShop4.jsp";
}
// String url="/EShop.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");// .microsoft.jdbc.sqlServer.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs","sa","");
Statement st=conn.createStatement();
st.setMaxRows(20);
String username;
String filmname="";
String account="";
String quantity="";
for (int i=0; i < buylist.size();i++) {
Film anOrder = (Film) buylist.elementAt(0);
filmname = String.valueOf(anOrder.getArtist()).toString();
account = String.valueOf(anOrder.getPrice()).toString();
quantity = String.valueOf(anOrder.getQuantity()).toString();
}
username = (String)session.getAttribute("sessionname");
String sqlStr ="INSERT INTO film (username,filmname,price,quantity) VALUES ('"+username+"','"+filmname+"','"+account+"','"+quantity+"')";
st.executeUpdate(sqlStr);
st.close();
conn.close();
}catch(SQLException sqle){
sqle.printStackTrace();
}
catch(Exception ex){
ex.printStackTrace();
}
float total =0;
for (int i=0; i< buylist.size();i++) {
Film anOrder = (Film) buylist.elementAt(i);
float price= anOrder.getPrice();
int qty = anOrder.getQuantity();
total += (price * qty);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf('.');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="/Checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private Film getFilm(HttpServletRequest req) {
//imagine if all this was in a scriptlet...ugly, eh?
String myfilm = req.getParameter("film");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myfilm,"|");
String star= t.nextToken();
String artist = t.nextToken();
String country = t.nextToken();
String price = t.nextToken();
price = price.replace('$',' ').trim();
Film fm = new Film();
fm.setStar(star);
fm.setArtist(artist);
fm.setCountry(country);
fm.setPrice((new Float(price)).floatValue());
fm.setQuantity((new Integer(qty)).intValue());
return fm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -