标题:请教一题关于jsp的session问题
只看楼主
Poppy辉
Rank: 1
来 自:云南曲靖
等 级:新手上路
帖 子:33
专家分:4
注 册:2016-11-5
结帖率:66.67%
已结贴  问题点数:10 回复次数:1 
请教一题关于jsp的session问题
老师布置的一道关于session的简易购物车 问题可能出现在了display.jsp中了  每次购物车的显示都是null null 还请好心人指点一下
Buy1.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <body>
        <form method="post"action="display.jsp"><h2>各种肉大甩卖,一律十块:</h2>
            <br><input type="checkbox"name="item"value="猪肉">猪肉
            <br><input type="checkbox"name="item"value="牛肉">牛肉
            <br><input type="checkbox"name="item"value="羊肉">羊肉
            <br><input type="submit"value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="reset"value="全部重写">
        </form>
        <p><a href="buy2.jsp">买点别的</a>&nbsp;&nbsp;&nbsp;
            <a href="display.jsp">查看购物车</a>
        </p>
        <% String meat[]=request.getParameterValues("item");
           if(meat!=null)
           {
               StringBuffer str=new StringBuffer();
               for(int i=0;i<meat.length;i++)
               {
                   str.append(meat[i]+"<br>");
               }
               session.setAttribute("meat", str);
           }
        %>
    </body>
</html>

Buy2.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <body>
        <form method="post"action="display.jsp"><h2>各种球大甩卖,一律八块:</h2>
            <br><input type="checkbox"name="item1"value="篮球">篮球
            <br><input type="checkbox"name="item1"value="足球">足球
            <br><input type="checkbox"name="item1"value="排球">排球
            <br><input type="submit"value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="reset"value="全部重写">
        </form>
        <p><a href="buy1.jsp">买点别的</a>&nbsp;&nbsp;&nbsp;
            <a href="display.jsp">查看购物车</a>
        </p>
        <% String ball[]=request.getParameterValues("item1");
           if(ball!=null)
           {
               StringBuffer str1=new StringBuffer();
               for(int i=0;i<ball.length;i++)
               {
                   str1.append(ball[i]+"<br>");
               }
               session.setAttribute("ball", str1);
           }
        %>
    </body>
</html>

Display.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%! public String handleStr(String s)
{
    try
{
    byte []b=s.getBytes("iso-8859-1");
    s= new String(b);
}
catch(Exception exp){}
 return  s;
}
%>
<html>
    <body>
        <% StringBuffer buyMeat=(StringBuffer)session.getAttribute("meat");
           StringBuffer buyBall=(StringBuffer)session.getAttribute("ball");
        %>
        <%
            String buyMeat1=(String)session.getAttribute("buyMeat");
            String buyBall1=(String)session.getAttribute("buyBall");
           out.print("你选择的结果是:"+"<br>");
        %>
        <%= handleStr(buyMeat1) %>
        <%= handleStr(buyBall1) %>
    </body>
</html>
搜索更多相关主题的帖子: action method 购物车 null 
2017-04-08 11:43
wlrjgzs
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:26
帖 子:212
专家分:1566
注 册:2017-4-10
得分:10 
你这代码明显有问题
2017-04-14 10:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-475890-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.522558 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved