📄 selectbookaction.java
字号:
package action;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import datamanage.po.PoBook;
public class SelectBookAction extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
int bookid=Integer.parseInt(request.getParameter("BookId"));
String bookname=request.getParameter("BookName");
String bookauthor=request.getParameter("BookAuthor");
String bookpublish=request.getParameter("BookPublish");
String bookpubdate=request.getParameter("BookPubDate");
Float bookprice=Float.parseFloat(request.getParameter("BookPrice"));
int booknum=Integer.parseInt(request.getParameter("BookNum"));
String bookPath=request.getParameter("BookPath");
PoBook pb=new PoBook();
pb.setBookId(bookid);
pb.setBookName(bookname);
pb.setBookAuthor(bookauthor);
pb.setBookPublish(bookpublish);
pb.setBookPubDate(bookpubdate);
pb.setBookPrice(bookprice);
pb.setBookPath(bookPath);
pb.setSelectNum(1);
List list = (List) request.getSession().getAttribute("list");
if (list == null) {
list = new ArrayList();
}
boolean flag = false;
for (int i = 0; i < list.size(); i++) {
PoBook op = (PoBook) list.get(i);
System.out.println("列表中有的书ID是" + op.getBookId());
System.out.println("你点的书ID是" + pb.getBookId());
if (op.getBookId() == pb.getBookId()) {
op.setSelectNum(op.getSelectNum() + 1);
flag = true;
}
}
if (flag == false) {
list.add(pb);
request.getSession().setAttribute("list", list);
}
float totalprice=0;
for (int i = 0; i < list.size(); i++) {
PoBook op = (PoBook) list.get(i);
totalprice=totalprice+(op.getBookPrice()*op.getSelectNum());
}
request.getSession().setAttribute("totalprice", totalprice);
response.sendRedirect("BookCarList.jsp");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -