📄 addgood.java
字号:
package xiaoshou.servlets;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import xiaoshou.classes.CGood;
import xiaoshou.javabeans.Operator;
public class AddGood extends HttpServlet {
//往购物车里加药品
/**
* Constructor of the object.
*/
public AddGood() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
*/
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
String[] sid=request.getParameterValues("checkbox");
//checkbox的value是药品的id,也是对应textbox的name
int n=sid.length;//选择的药品种类的个数(明细条数)
ArrayList list=new ArrayList();
for(int i=0;i<n;i++){
String snum=request.getParameter(sid[i]);//对应textbox的value,即药品数量
int num=Integer.parseInt(snum);//转化为数字
CGood good=Operator.queryGood(sid[i]);
good.setNum(num);//设置定购药品数量
list.add(good);
}
/*以下代码把选的药品加入购物车中,若两次的药品相同,则合并其数量*/
if(list.size()>0){
ArrayList lists=(ArrayList)request.getSession(true).getAttribute("lists");
if(lists!=null){//
ArrayList same=new ArrayList();//用于暂存相同的药品
for(int i=0;i<lists.size();i++){
for(int j=0;j<list.size();j++){
CGood good1=null,good2=null;
good1=(CGood)lists.get(i);
good2=(CGood)list.get(j);
if(good1.getId()==good2.getId()){
good1.setNum(good1.getNum()+good2.getNum());
same.add(good2);//把相同的药品放入same列表
}
}
}
if(same.size()>0)
list.removeAll(same);//清掉相同的药品
if(list.size()>0)
lists.addAll(list);//添加药品到购物车
request.getSession(true).removeAttribute("lists");
}else{
lists=list;
}
request.getSession(true).setAttribute("lists", lists);
}
/* ******************************************/
request.getRequestDispatcher("/xiaoshou/applyGoods.jsp").forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -