flowerindexbuyservlet.java

来自「精美的画面。完善的功能」· Java 代码 · 共 89 行

JAVA
89
字号
package Flower.view;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
import Flower.model.*;
import java.sql.*;
import java.util.*;
public class FlowerIndexBuyServlet extends HttpServlet 
{
  private static final String CONTENT_TYPE = "text/html; charset=GBK";

  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    this.doPost(request,response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    String action=request.getParameter("action");
    HttpSession session=request.getSession();
    if(session.getAttribute("cart")==null)//判断session对象存不存在
    {
      session.setAttribute("cart",new shoppingCartBean());
    }
    shoppingCartBean cart=(shoppingCartBean)session.getAttribute("cart");
    if(action.equals("find"))
    {
      String flowername=request.getParameter("goodsname");
      Connection con=null;
      PreparedStatement pstmt=null;
      ResultSet rs=null;
      Connection con1=null;
      PreparedStatement pstmt1=null;
      ResultSet rs1=null;
      String flowerid="";
      String goodsid="";
      long num=1;
      double price=0;
      try
      {
         con=DBConnect.getConnection();
         pstmt=con.prepareStatement("select * from Flowerinfo where Flowername=?");
         pstmt.setString(1,flowername);
         rs=pstmt.executeQuery();
         while(rs.next())
         {
           flowerid=rs.getString(1);
         }
         rs.close();
         pstmt.close();
         con.close();
         con1=DBConnect.getConnection();
         pstmt1=con1.prepareStatement("select Goodsinfo.goodsid,goodsinfo.RETAILPRICE from FlowerInfo,Goodsinfo where FLowerinfo.flowerid=Goodsinfo.FLOWERID and Flowerinfo.FLowerid=?");
         pstmt1.setString(1,flowerid);
         rs1=pstmt1.executeQuery();
         while(rs1.next())
         {
           goodsid=rs1.getString(1);
           price=rs1.getDouble(2);
         }
         rs1.close();
         pstmt1.close();
         con1.close();
      }
      catch(Exception ex)
      {
        ex.printStackTrace();
      }
      if(goodsid!=null&!goodsid.equals(""))
      {
        shoppingCart p=new shoppingCart();
        p.setGoodsid(goodsid);
        p.setPrice(price);
        p.setNum(num);
        cart.addItem(p);
      }
      response.sendRedirect("FlowerIndex.jsp");
    }
    out.close();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?