📄 controlservlet.java
字号:
package com.tarena.develop.control;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.tarena.develop.common.BusinessFactory;
import com.tarena.develop.common.JDBCUtil;
import com.tarena.develop.entity.Order;
import com.tarena.develop.entity.OrderLine;
import com.tarena.develop.entity.Product;
import com.tarena.develop.entity.User;
import com.tarena.develop.service.impl.OrderService;
import com.tarena.develop.service.impl.OrderServiceImpl;
import com.tarena.develop.service.impl.ProductService;
import com.tarena.develop.service.impl.ProductServiceImpl;
import com.tarena.develop.service.impl.ShopCart;
import com.tarena.develop.service.impl.UserService;
import com.tarena.develop.service.impl.UserServiceImpl;
public class ControlServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String path = request.getServletPath();
String username = request.getParameter("username");
String password = request.getParameter("password");
String token = request.getParameter("token");
System.out.print("lalal");
// UserService us = new UserServiceImpl();
// User u = us.getUser(username, password);
if("/test/user/dologin.do".equals(path)){
System.out.print("^^^^^^^^^^");
String error = null;
UserService us = new UserServiceImpl();
User u = us.getUser(username, password);
if(u!=null){
HttpSession session = request.getSession();
session.setAttribute("user", u);
if(token.equals(String.valueOf(session.getAttribute("token")))){
//得到容器环境
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/user/welcome.jsp");
//转发
dispatcher.forward(request, response);
}else{
error = "对不起你的验证码错误!";
request.setAttribute("error", error);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/global/error.jsp");
//转发
dispatcher.forward(request, response);
}
}else{
error = "对不起你的用户名或密码错误!";
request.setAttribute("error", error);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/global/error.jsp");
//转发
dispatcher.forward(request, response);
}
}
//查看用户的详细信息
if("/test/user/userinfo.do".equals(path)){
System.out.print("haha");
// HttpSession session = request.getSession(false);
// session.setAttribute("user", u);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/user/userinfo.jsp");
//转发
dispatcher.forward(request, response);
}
if("/login.do".equals(path)){
HttpSession session = request.getSession();
session.setAttribute("token", JDBCUtil.getToken());
ProductService ps = (ProductService) BusinessFactory.getBusiness(ProductService.class.getName());
List<Product> products = ps.getProducts();
request.setAttribute("products", products);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp");
//转发
dispatcher.forward(request, response);
}
if("/test/order/orderlist.do".equals(path)){
HttpSession session = request.getSession(false);
User u = (User) session.getAttribute("user");
OrderService os = new OrderServiceImpl();
List<Order> orders = os.getOrders(u);
request.setAttribute("orders", orders);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/order/orderlist.jsp");
//转发
dispatcher.forward(request, response);
}
if("/test/order/orderdetail.do".equals(path)){
HttpSession session = request.getSession(false);
User u = (User) session.getAttribute("user");
String oid = request.getParameter("orderid");
OrderService os = new OrderServiceImpl();
Order order = os.getOrder(Integer.parseInt(oid), u);
List<OrderLine> orderlines = os.getOrderLine(order);
// List<OrderLine> orderlines = new ArrayList<OrderLine>();
// OrderLine ol = new OrderLine();
// Order order1 = new Order();
// order1.setOrderid(1);
// order1.setOrdername("cc1111");
// order1.setOrderlines(orderlines)
// Product product = new ProductServiceImpl().find(1);
// ol.setOrder(order1);
// ol.setOrderlineid(1);
// ol.setProduct(product);
// ol.setAmount(3);
// orderlines.add(ol);
request.setAttribute("orderlines",orderlines );
request.setAttribute("order", order);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/order/orderdetail.jsp");
//转发
dispatcher.forward(request, response);
}
if("/shopcart/addtoshopcart.do".equals(path)){
String pid = request.getParameter("pid");
HttpSession session = request.getSession(true);
ShopCart sc = (ShopCart) session.getAttribute("sc");
System.out.print(sc);
Product pro = new ProductServiceImpl().find(Integer.parseInt(pid));
sc.addProduct(pro);
ServletContext context = this.getServletContext();
//创建请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/test/shopcart/shopcart.jsp");
//转发
dispatcher.forward(request, response);
}
// if("/order/orderconfirm.do".equals(path)){
//
// ServletContext context = this.getServletContext();
// //创建请求转发器
// RequestDispatcher dispatcher = context.getRequestDispatcher("/test/order/orderconfirm.jsp");
// //转发
// dispatcher.forward(request, response);
// }
// if("/shopcart/toshopcart.do".equals(path)){
// ServletContext context = this.getServletContext();
// //创建请求转发器
// RequestDispatcher dispatcher = context.getRequestDispatcher("/test/shopcart/shopcart.jsp");
// //转发
// dispatcher.forward(request, response);
// }
// if("/order/ordercommit.do".equals(path)){
// GregorianCalendar gc = new GregorianCalendar();
// String year = String.valueOf(gc.get(Calendar.YEAR));
// String month = String.valueOf(gc.get(Calendar.MONTH));
// String day = String.valueOf(gc.get(Calendar.DAY_OF_MONTH));
// String hour = String.valueOf(gc.get(Calendar.HOUR_OF_DAY));
// String second = String.valueOf(gc.get(Calendar.SECOND));
// String payway = (String) request.getAttribute("payway");
// HttpSession session = request.getSession(false);
// User user = (User) session.getAttribute("user");
// ShopCart sc = (ShopCart) session.getAttribute("sc");
//
// OrderService os = new OrderServiceImpl();
// Order order = new Order();
// order.setPayway(payway);
// order.setCreatedate(new Date(new Long(1111111)));
// order.setOrdername("CCS"+year+month+day+hour+second);
// order.setStateid("待处理");
// order.setCost(sc.getCost());
//
//
// os.addOrder(order,user.getId());
//
//
// ServletContext context = this.getServletContext();
// //创建请求转发器
// RequestDispatcher dispatcher = context.getRequestDispatcher("/test/order/orderlist.do");
// //转发
// dispatcher.forward(request, response);
// }
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doGet(request,response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -