📄 actionservlet.java
字号:
package com.qyg.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
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.qyg.tools.Card;
import com.qyg.shop.product.*;
import java.util.*;
public class ActionServlet extends HttpServlet {
Card card;
/**
* Constructor of the object.
*/
public ActionServlet() {
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 {
HttpSession session = request.getSession();
ProductDAO dao=new ProductManageDAO();
String action=request.getParameter("action");
String orderby = request.getParameter("orderby");
//商品查询中 购买导航 处理
if(action!=null){
if(action.equals("lead")){
response.sendRedirect("../product.jsp?action=lead");
}
//ID查询
else if(action.equals("idSearch")){
String ID = request.getParameter("ID");
try{
response.sendRedirect("../product.jsp?action=id&ID="+ID);
}catch(NumberFormatException e){
System.out.println("NumberFormatError");
response.sendRedirect("../page/goodssearch.jsp?errInfo=idnum_error");
return;
}
}
//商品查询中 接受用户输入框的处理
else if(orderby!=null){
String orderby2 = "product_id";
if(orderby.equals("商品ID号")){
orderby2 = "product_id";
}else if(orderby.equals("商品名称")){
orderby2 = "product_name";
}else if(orderby.equals("商品价格")){
orderby2 = "product_price";
}else if(orderby.equals("商品种类")){
orderby2 = "product_cId";
}
//关键字查询
if(action.equals("keywordSearch")){
String keyword = request.getParameter("keyword");
session.setAttribute("keyword", keyword);
response.sendRedirect("../product.jsp?action=key");
}
//价格范围查询
else if(action.equals("priceSearch")){
String lowprice = request.getParameter("lowprice");
String topprice = request.getParameter("topprice");
try{
session.setAttribute("lowprice", lowprice);
session.setAttribute("topprice", topprice);
response.sendRedirect("../product.jsp?action=price");
}catch(NumberFormatException e){
System.out.println("NumberFormatError");
response.sendRedirect("../page/goodssearch.jsp?errInfo=pricenum_error");
return;
}
}
session.setAttribute("orderby", orderby2);
}
//用户点击 购买 的事件处理
else if(action.equals("buy")){
int product_id=Integer.parseInt(request.getParameter("productid"));
Product p1=dao.getProductById(product_id);
session = request.getSession();
card=(Card)session.getAttribute("mycard");
if(card!=null){
card.addProduct(p1);
session.setAttribute("mycard",card);
}
response.sendRedirect("../mycard.jsp");
}
//购物车中点击 删除 的处理
else if(action.equals("del")){
int product_id=Integer.parseInt(request.getParameter("productid"));
Product p1=dao.getProductById(product_id);
session = request.getSession();
System.out.println("no start get session");
card=(Card)session.getAttribute("mycard");
if(card==null){
card=new Card();
}
card.delProduct(p1);
System.out.println("del product");
session.setAttribute("mycard",card);
System.out.println("into session");
response.sendRedirect("../mycard.jsp");
}
//购物车中点击 增加 的处理
else if(action.equals("add")){
int product_id=Integer.parseInt(request.getParameter("productid"));
Product p1=dao.getProductById(product_id);
session = request.getSession();
card=(Card)session.getAttribute("mycard");
card.addProduct(p1);
session.setAttribute("mycard",card);
response.sendRedirect("../mycard.jsp");
}
//购物车中点击 结账 的处理
else if(action.equals("over")){
double sum=0;
session = request.getSession();
card=(Card)session.getAttribute("mycard");
ArrayList list=card.getPlist();
for(int i=0;i<list.size();i++){
Product p = (Product)list.get(i);
sum += p.getProduct_price()*p.getProduct_cId();
}
session.setAttribute("result", sum);
session.setAttribute("mycard",card);
response.sendRedirect("../mycard.jsp");
}
//最新产品
else if(action.equals("newest")){
response.sendRedirect("../product.jsp?action=newest");
}
//消费金额记录查询页面处理
else if(action.equals("select")){
String radio = request.getParameter("radio");
//消费记录查询中 没有选择所查询的消费记录时
if(radio==null){
response.sendRedirect("../page/select.jsp?radio=null");
}
//消费记录查询中 点击最新消费查询
else if(radio.equals("one")){
response.sendRedirect("../page/select.jsp?radio=one");
}
//消费记录查询中 历史记录查询
else if(radio.equals("all")){
response.sendRedirect("../page/select.jsp?radio=all");
}
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -