慕斯卡4106679
2016-01-13 23:29
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ItemsDAO;
import entity.Cart;
import entity.Items;
public class CartServlet extends HttpServlet {
private String action ; //表示购物车的动作 ,add,show,delete
//商品业务逻辑类的对象
private ItemsDAO idao = new ItemsDAO();
/**
* Constructor of the object.
*/
public CartServlet() {
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 {
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 {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
if(request.getParameter("action")!=null)
{
this.action = request.getParameter("action");
if(action.equals("add")) //如果是添加商品进购物车
{
if(addToCart(request,response))
{
request.getRequestDispatcher("/success.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
}
}
if(action.equals("show"))//如果是显示购物车
{
request.getRequestDispatcher("/cart.jsp").forward(request, response);
}
if(action.equals("delete")) //如果是执行删除购物车中的商品
{
if(deleteFromCart(request,response))
{
request.getRequestDispatcher("/cart.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("/cart.jsp").forward(request, response);
}
}
}
}
//添加商品进购物车的方法
private boolean addToCart(HttpServletRequest request, HttpServletResponse response)
{
String id = request.getParameter("id");
String number = request.getParameter("num");
Items item = idao.getItemsById(Integer.parseInt(id));
//是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象
if(request.getSession().getAttribute("cart")==null)
{
Cart cart = new Cart();
request.getSession().setAttribute("cart",cart);
}
Cart cart = (Cart)request.getSession().getAttribute("cart");
if(cart.addGoodsInCart(item, Integer.parseInt(number)))
{
return true;
}
else
{
return false;
}
}
//从购物车中删除商品
private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response)
{
String id = request.getParameter("id");
Cart cart = (Cart)request.getSession().getAttribute("cart");
Items item = idao.getItemsById(Integer.parseInt(id));
if(cart.removeGoodsFromCart(item))
{
return true;
}
else
{
return false;
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
private boolean addToCart(HttpServletRequest request, HttpServletResponse response)
为什么这个方法的参数是HttpServletRequest request, HttpServletResponse response,不太懂,而且为什么返回值要弄成布尔类型?
1.使用HttpServletRequest request和HttpServletResponse response作为参数是为了方便在addToCart方法中获得传过来的商品id和购买数量。即使用request.getParameter方法直接获得参数值。当然,你也可以这样
if(action.equals("add")) //如果是添加商品进购物车
{
String id = request.getParameter("id");
String number = request.getParameter("num");
addToCart(id, number);
}
//。。。。。。
private boolean addToCart(String id, String number)
{
//。。。。
}
2.方法返回布尔类型是为了使程序更好的判断是否正确的将商品加入了购物车,以便针对结果更好的处理后继流程,使程序更健壮。
JAVA遇见HTML——Servlet篇
160613 学习 · 1029 问题
相似问题