第二个问题:
我做的一个购物车,然后如果客户不登陆,点击购物车的话就直接转到登陆页面,
现在问题是不登陆,点击购物车会报错:
java.lang.IllegalStateException
org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:352)
org.apache.jsp.book_005fstore.shopcart_jsp._jspService(shopcart_jsp.java:301)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

解决方案 »

  1.   

    购物车的代码是这样的:
    <%@ page contentType="text/html;charset=GB2312" %>
    <%@ page language="java" import="java.sql.*" %>
    <jsp:useBean id="workM" scope="page" class="test.faq" />
    <%@ include file="function.inc.jsp"%>
    <%
    if(session.getAttribute("username")==null||session.getAttribute("username")=="")
    {
    response.sendRedirect("../member/login.jsp?url="+request.getRequestURI());
    }%><%!
    public String getStr(String str)
    {
    try
    {
    String temp_p=str;
    byte[] temp_t=temp_p.getBytes("GBK");
    String temp=new String(temp_t,"ISO8859_1");
    return temp;
    }
    catch(Exception e)
    {  
    }
    return "null";
    }
    %>        
    <%
    String book_id=request.getParameter("book_id");
    //是否已经选择该货物
        String sql="select ID from orders where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
    ResultSet RS=workM.executeQuery(sql);
    int rowscount=0;
    try
    {
    while(RS.next())
    {
    rowscount++;
    }
    }
    catch(Exception e)
    { }
    //把货物放入购物车
    if(rowscount==0)
    {
    String sqlBook1="select book.* from book where id="+book_id;
    ResultSet RSBook1=workM.executeQuery(sqlBook1);
    while(RSBook1.next())
    {
    String sqlCart="insert into  orders(user_name,book_id,book_number,status,goods_price)  Values('"+session.getAttribute("username")+"','"+book_id+"','1','0','"+RSBook1.getDouble("price")+"')";
    workM.executeQuery(sqlCart);
    }
    }
    else
    {
    String sqlBook2="select book.* from book where id="+book_id;
    ResultSet RSBook2=workM.executeQuery(sqlBook2);
    while(RSBook2.next())
    {
     String sqlAdd="update orders set book_number=book_number+1,goods_price="+RSBook2.getDouble("price")+" where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
    workM.executeQuery(sqlAdd);
    }
    }
     /*
    int count; 
    count=0;
       String sqlBook3="select * from price where user_name="+session.getAttribute("username");
    ResultSet RSBook3=workM.executeQuery(sqlBook3);
    while(RSBook3.next())
    {
    count=count+1;
      }
    if(count==0)
    {
    String sqlBook4="select * from orders where user_name="+session.getAttribute("username");
    ResultSet RSBook4=workM.executeQuery(sqlBook4);
    while(RSBook4.next())
    {
    String sqlPrice="insert into price(user_name,goods_price,total_price) values('"+session.getAttribute("username")+"','"+(double)(RSBook4.getDouble("goods_price")*RSBook4.getInt("book_number"))+"','goods_price+5')";
    workM.executeQuery(sqlPrice);
    }
    }
    else
    {
    String sqlBook5="select * from orders where user_name="+session.getAttribute("username");
    ResultSet RSBook5=workM.executeQuery(sqlBook5);
    while(RSBook5.next())
    {
    String sqlPrice="update price set goods_price="+(double)(RSBook5.getDouble("goods_price")*RSBook5.getInt("book_number"))+",total_price=goods_price+5";
    workM.executeQuery(sqlPrice);
    }
    }*/
    response.sendRedirect("cart.jsp");
    %>                
    <html>
    <head>
    <title>[详细资料]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body bgcolor="#FFFFFF">
    <div align="center">
      <table width="100%" border="0" bgcolor="#CCCCFF">
        <tr>
          <td>
            <div align="center"><b><font color="#FF0000">购 物 车</font></b> </div>
          </td>
        </tr>
      </table>
      
      <p>您的购物车中包含以下货物:</p>
      <table width="100%" border="0" align="center">
        <tr bgcolor="#FFCCCC"> 
          <td width="15%"> 
            <div align="center"><font color="#0000FF">购买数量</font></div>
          </td>
          <td width="56%"> 
            <div align="center"><font color="#0000FF">书名</font></div>
          </td>
          <td width="13%"> 
            <div align="center"><font color="#0000FF">单价</font></div>
          </td>
          <td width="16%"> 
            <div align="center"><font color="#0000FF">总价格</font></div>
          </td>
        </tr>
    <%
    String sqlList="select * from orders where user_name='"+session.getAttribute("username")+"' and status=0";
    ResultSet RSList=workM.executeQuery(sqlList);
    try
    {
    while(RSList.next())
    {
    int b_num;
    b_num=RSList.getInt("book_number");
    %>
        <tr bgcolor="#CCFFCC"> 
          <td width="15%"> 
            <div align="center"> <font color="#0000FF"> 
              <input type="text" name="book_number" size="4" value=<%=b_num%>>
              </font></div>
          </td>
      <%
       String sqlBook="select book.* from book where id="+RSList.getInt("book_id");
    ResultSet RSBook=workM.executeQuery(sqlBook);
    while(RSBook.next())
    {
      %>
          <td width="56%"> 
            <div align="center"><font color="#0000FF"><%=getStr(RSBook.getString("name"))%></font></div>
          </td>
          <td width="13%"> 
      <%
       double price;
    price=RSBook.getDouble("price");
      %>
            <div align="center"><font color="#0000FF"><%=price%>元</font></div>
          </td>
     
          <td width="16%"> 
            <div align="center"><font color="#0000FF">&yen;<%=(double)price*b_num%>元</font></div>
          </td>
       <%}%>
        </tr>
        <tr bgcolor="#CCCCFF"> 
          <td colspan="3"> 
            <div align="center"><font color="#0000FF">货物价格</font></div>
          </td>
          <td width="16%"> 
            <div align="center"><font color="#0000FF">&yen;</font></div>
          </td>
        </tr>
    <%
    }
    }
    catch(Exception e)
    {
    }
    %>
        <tr> 
          <td height="18" colspan="3" bgcolor="#CCCC99"><font color="#0000FF">&nbsp; 
            </font> 
            <div align="center"><font color="#0000FF">运输费用</font></div>
          </td>
          <td height="18" width="16%" bgcolor="#CCCC99"> 
            <div align="center"><font color="#0000FF">&yen;</font></div>
          </td>
        </tr>
        <tr bgcolor="#66FF66"> 
          <td colspan="3"> 
            <div align="center"><b><font color="#FF0000">总费用</font></b></div>
          </td>
          <td width="16%"> 
            <div align="center"><b><font color="#FF0000">&yen;</font></b></div>
          </td>
        </tr>
     <tr align="center"> 
          <td colspan="4"> 
     <form name="form1" method="post" >
        <input type="button" value="继续购物" onclick="javascript:self.close();">
        <input type="submit" name="Submit" value="重新计算价格">
        <input type="submit" name="Submit2" value="填写订单">
      </form>
      </td>
        </tr>
      </table>
      <p>&nbsp;</p>
    </div></body>
    </html>
    大家看看用什么办法解决呀
    在线等
      

  2.   

    购物车的代码是这样的:
    <%@ page contentType="text/html;charset=GB2312" %>
    <%@ page language="java" import="java.sql.*" %>
    <jsp:useBean id="workM" scope="page" class="test.faq" />
    <%@ include file="function.inc.jsp"%>
    <%
    if(session.getAttribute("username")==null||session.getAttribute("username")=="")
    {
    response.sendRedirect("../member/login.jsp?url="+request.getRequestURI());
    }%><%!
    public String getStr(String str)
    {
    try
    {
    String temp_p=str;
    byte[] temp_t=temp_p.getBytes("GBK");
    String temp=new String(temp_t,"ISO8859_1");
    return temp;
    }
    catch(Exception e)
    {  
    }
    return "null";
    }
    %>        
    <%
    String book_id=request.getParameter("book_id");
    //是否已经选择该货物
        String sql="select ID from orders where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
    ResultSet RS=workM.executeQuery(sql);
    int rowscount=0;
    try
    {
    while(RS.next())
    {
    rowscount++;
    }
    }
    catch(Exception e)
    { }
    //把货物放入购物车
    if(rowscount==0)
    {
    String sqlBook1="select book.* from book where id="+book_id;
    ResultSet RSBook1=workM.executeQuery(sqlBook1);
    while(RSBook1.next())
    {
    String sqlCart="insert into  orders(user_name,book_id,book_number,status,goods_price)  Values('"+session.getAttribute("username")+"','"+book_id+"','1','0','"+RSBook1.getDouble("price")+"')";
    workM.executeQuery(sqlCart);
    }
    }
    else
    {
    String sqlBook2="select book.* from book where id="+book_id;
    ResultSet RSBook2=workM.executeQuery(sqlBook2);
    while(RSBook2.next())
    {
     String sqlAdd="update orders set book_number=book_number+1,goods_price="+RSBook2.getDouble("price")+" where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"' and status=0";
    workM.executeQuery(sqlAdd);
    }
    }
     /*
    int count; 
    count=0;
       String sqlBook3="select * from price where user_name="+session.getAttribute("username");
    ResultSet RSBook3=workM.executeQuery(sqlBook3);
    while(RSBook3.next())
    {
    count=count+1;
      }
    if(count==0)
    {
    String sqlBook4="select * from orders where user_name="+session.getAttribute("username");
    ResultSet RSBook4=workM.executeQuery(sqlBook4);
    while(RSBook4.next())
    {
    String sqlPrice="insert into price(user_name,goods_price,total_price) values('"+session.getAttribute("username")+"','"+(double)(RSBook4.getDouble("goods_price")*RSBook4.getInt("book_number"))+"','goods_price+5')";
    workM.executeQuery(sqlPrice);
    }
    }
    else
    {
    String sqlBook5="select * from orders where user_name="+session.getAttribute("username");
    ResultSet RSBook5=workM.executeQuery(sqlBook5);
    while(RSBook5.next())
    {
    String sqlPrice="update price set goods_price="+(double)(RSBook5.getDouble("goods_price")*RSBook5.getInt("book_number"))+",total_price=goods_price+5";
    workM.executeQuery(sqlPrice);
    }
    }*/
    response.sendRedirect("cart.jsp");
    %>                
    <html>
    <head>
    <title>[详细资料]</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body bgcolor="#FFFFFF">
    <div align="center">
      <table width="100%" border="0" bgcolor="#CCCCFF">
        <tr>
          <td>
            <div align="center"><b><font color="#FF0000">购 物 车</font></b> </div>
          </td>
        </tr>
      </table>
      
      <p>您的购物车中包含以下货物:</p>
      <table width="100%" border="0" align="center">
        <tr bgcolor="#FFCCCC"> 
          <td width="15%"> 
            <div align="center"><font color="#0000FF">购买数量</font></div>
          </td>
          <td width="56%"> 
            <div align="center"><font color="#0000FF">书名</font></div>
          </td>
          <td width="13%"> 
            <div align="center"><font color="#0000FF">单价</font></div>
          </td>
          <td width="16%"> 
            <div align="center"><font color="#0000FF">总价格</font></div>
          </td>
        </tr>
    <%
    String sqlList="select * from orders where user_name='"+session.getAttribute("username")+"' and status=0";
    ResultSet RSList=workM.executeQuery(sqlList);
    try
    {
    while(RSList.next())
    {
    int b_num;
    b_num=RSList.getInt("book_number");
    %>
        <tr bgcolor="#CCFFCC"> 
          <td width="15%"> 
            <div align="center"> <font color="#0000FF"> 
              <input type="text" name="book_number" size="4" value=<%=b_num%>>
              </font></div>
          </td>
      <%
       String sqlBook="select book.* from book where id="+RSList.getInt("book_id");
    ResultSet RSBook=workM.executeQuery(sqlBook);
    while(RSBook.next())
    {
      %>
          <td width="56%"> 
            <div align="center"><font color="#0000FF"><%=getStr(RSBook.getString("name"))%></font></div>
          </td>
          <td width="13%"> 
      <%
       double price;
    price=RSBook.getDouble("price");
      %>
            <div align="center"><font color="#0000FF"><%=price%>元</font></div>
          </td>
     
          <td width="16%"> 
            <div align="center"><font color="#0000FF">&yen;<%=(double)price*b_num%>元</font></div>
          </td>
       <%}%>
        </tr>
        <tr bgcolor="#CCCCFF"> 
          <td colspan="3"> 
            <div align="center"><font color="#0000FF">货物价格</font></div>
          </td>
          <td width="16%"> 
            <div align="center"><font color="#0000FF">&yen;</font></div>
          </td>
        </tr>
    <%
    }
    }
    catch(Exception e)
    {
    }
    %>
        <tr> 
          <td height="18" colspan="3" bgcolor="#CCCC99"><font color="#0000FF">&nbsp; 
            </font> 
            <div align="center"><font color="#0000FF">运输费用</font></div>
          </td>
          <td height="18" width="16%" bgcolor="#CCCC99"> 
            <div align="center"><font color="#0000FF">&yen;</font></div>
          </td>
        </tr>
        <tr bgcolor="#66FF66"> 
          <td colspan="3"> 
            <div align="center"><b><font color="#FF0000">总费用</font></b></div>
          </td>
          <td width="16%"> 
            <div align="center"><b><font color="#FF0000">&yen;</font></b></div>
          </td>
        </tr>
     <tr align="center"> 
          <td colspan="4"> 
     <form name="form1" method="post" >
        <input type="button" value="继续购物" onclick="javascript:self.close();">
        <input type="submit" name="Submit" value="重新计算价格">
        <input type="submit" name="Submit2" value="填写订单">
      </form>
      </td>
        </tr>
      </table>
      <p>&nbsp;</p>
    </div></body>
    </html>
    大家看看用什么办法解决呀
    在线等
      

  3.   

    最好是把java代码和jsp尽量的分开,这么看眼睛都花了,也不好调试!