为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我调用这个Servlet 会执行2次

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.imooc.DAO.ItemsDao;
import com.imooc.model.Cart;
import com.imooc.model.Items;

/**
 * Servlet implementation class CartServlet
 */
public class CartServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 private String action;//表示购物车的动作
 ItemsDao dao = new ItemsDao();
   
   
    public CartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  System.out.println("我是GRT ,我执行了");
  response.setContentType("text/html; charset=utf-8" );

  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);
    }
   }else {
    request.getRequestDispatcher("../cart.jsp").forward(request, response);

   }
  }
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 doGet(request, response);
 }

 

 private boolean addToCart(HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  
  String id = request.getParameter("id");
  String number = request.getParameter("num");
  Items item = dao.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.addCommodity(item, Integer.parseInt(number))) {
    
    return true;
   }else {
    return false;
   }
  
  
  
 }

}


输出结果

我是GRT ,我执行了
我是GRT ,我执行了


正在回答

3 回答

同问?

0 回复 有任何疑惑可以回复我~

<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.imooc.model.Items"%>
<%@page import="com.imooc.DAO.ItemsDao"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'details.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <link href="css/main.css" rel="stylesheet" type="text/css">
 <script type="text/javascript" src="js/lhgcore.js"></script>
    <script type="text/javascript" src="js/lhgdialog.js"></script>
    <script type="text/javascript">
      function selflog_show(id)
      {
       
         var num =  document.getElementById("number").value;
         J.dialog.get({id: 'haoyue_creat',title: '购物成功',width: 600,height:400, link: '<%=path%>/servlet/CartServlet?id='+id+'&num='+num+'&action=add', cover:true});
      }
      function add()
      {
         var num = parseInt(document.getElementById("number").value);
         if(num<100)
         {
            document.getElementById("number").value = ++num;
         }
      }
      function sub()
      {
         var num = parseInt(document.getElementById("number").value);
         if(num>1)
         {
            document.getElementById("number").value = --num;
         }
      }
    
    </script>
 
    <style type="text/css">
    hr{
     
      border-color:FF7F00;
    }
   
    div{
       float:left;
       margin-left: 30px;
       margin-right:30px;
       margin-top: 5px;
       margin-bottom: 5px;
     
    }
    div dd{
       margin:0px;
       font-size:10pt;
    }
    div dd.dd_name
    {
       color:blue;
    }
    div dd.dd_city
    {
       color:#000;
    }
    div #cart
    {
      margin:0px auto;
      text-align:right;
    }
    span{
      padding:0 2px;border:1px #c0c0c0 solid;cursor:pointer;
    }
    a{
       text-decoration: none;
    }
 </style>
  </head>
 
  <body>
    <h1>商品详情</h1>
    <a href="index.jsp">首页</a> >> <a href="index.jsp">商品列表</a>
    <hr>
    <center>
      <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <!-- 商品详情 -->
          <%
             ItemsDao itemDao = new ItemsDao();
             Items item = itemDao.getItemsBYid(Integer.parseInt(request.getParameter("id")));
             if(item!=null)
             {
          %>
          <td width="70%" valign="top">
             <table>
               <tr>
                 <td rowspan="5"><img src="images/<%=item.getPicture()%>" width="200" height="160"/></td>
               </tr>
               <tr>
                 <td><B><%=item.getName() %></B></td>
               </tr>
               <tr>
                 <td>产地:<%=item.getCity()%></td>
               </tr>
               <tr>
                 <td>价格:<%=item.getPrice() %>¥</td>
               </tr>
               <tr>
                 <td>购买数量:<span id="sub" onclick="sub();">-</span><input type="text" id="number" name="number" value="1" size="2"/><span id="add" onclick="add();">+</span></td>
               </tr>
             </table>
             <div id="cart">
               <img src="images/buy_now.png"><a href="javascript:selflog_show(<%=item.getId()%>)"><img src="images/in_cart.png"></a><a href="servlet/CartServlet?action=show"><img src="images/view_cart.jpg"/></a>
             </div>
          </td>
          <%
            }
          %>
          <%
              String list ="";
              //从客户端获得Cookies集合
              Cookie[] cookies = request.getCookies();
              //遍历这个Cookies集合
              if(cookies!=null&&cookies.length>0)
              {
               for(Cookie c:cookies)
               {
                   if(c.getName().equals("ListViewCookie"))
                   {
                      list =  URLDecoder.decode(c.getValue(), "UTF-8");
                   }
               }
           }
             
              list+=request.getParameter("id")+",";
              //如果浏览记录超过1000条,清零.
              String[] arr = list.split(",");
              if(arr!=null&&arr.length>0)
              {
                  if(arr.length>=1000)
                  {
                      list="";
                  }
              }
              Cookie cookie = new Cookie("ListViewCookie",URLEncoder.encode(list, "UTF-8"));
              response.addCookie(cookie);
         
          %>
          <!-- 浏览过的商品 -->
          <td width="30%" bgcolor="#EEE" align="center">
             <br>
             <b><font color="#FF7F00">您浏览过的商品</font></b><br>
             <!-- 循环开始 -->
             <%
                ArrayList<Items> itemlist = itemDao.getViewList(list);
                if(itemlist!=null&&itemlist.size()>0 )
                {
                   for(Items i:itemlist)
                   {
                        
             %>
             <div>
             <dl>
               <dt>
                 <a href="details.jsp?id=<%=i.getId()%>"><img src="images/<%=i.getPicture() %>" width="120" height="90" border="1"/></a>
               </dt>
               <dd class="dd_name"><%=i.getName() %></dd>
               <dd class="dd_city">产地:<%=i.getCity() %>&nbsp;&nbsp;价格:<%=i.getPrice() %> ¥ </dd>
             </dl>
             </div>
             <%
                   }
                }
             %>
             <!-- 循环结束 -->
          </td>
        </tr>
      </table>
    </center>
  </body>
</html>


0 回复 有任何疑惑可以回复我~

去看看jsp那边是不是请求2次;你这个Servlet看起来没问题。

0 回复 有任何疑惑可以回复我~
#1

qq_逆时针_1 提问者

jsp页面在楼下 你看看 ,我感觉也没错啊
2017-06-01 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我调用这个Servlet 会执行2次

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信