jsp ----session

jsp—-session

今天用购物车实例学习session,主要有三个页面:

  1. name.jsp:用户输入自己的姓名,页面将用户的姓名保证保存至session。
  2. book.jsp:用户挑选想要购买的图书,页面将用户选择保存至session。
  3. shopcar:用户的购物车程序,用来展示用户之前所填写和选择的内容,并计算出总价格。
    贴代码:
    book.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<a href="name.jsp">输入姓名</a><br>
<a href="book.jsp">选择图书</a><br>
<a href="shopcar.jsp">购物车</a><br>
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>选择图书</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">
    -->
  </head>
  <body>
     <h3>选择购买的书籍:</h3>
     <form action="" method=post name="book">
     <input type="checkbox" name="bookname" value="三字经36.2元">三字经36.2元
     <input type="checkbox" name="bookname" value="百家姓36.2元">百家姓36.2元
     <input type="checkbox" name="bookname" value="唐诗三百首36.2元">唐诗三百首36.2元
     <input type="checkbox" name="bookname" value="西游记36.2元">西游记36.2元
     <input type="checkbox" name="bookname" value="三国演义36.2元">三国演义36.2元
     <input type="checkbox" name="bookname" value="红楼梦36.2元">红楼梦36.2元<br>
     <input type="submit" value="加入购物车">
     </form>
     <% String book[]=request.getParameterValues("bookname"); 
     if(book!=null){
     StringBuffer str=new StringBuffer();
     for(int i=0;i<book.length;i++){
     str.append(book[i]+"<br>");
     }
     session.setAttribute("book",str);
     }
     %>
  </body>
</html>

name.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<a href="name.jsp">输入姓名</a><br>
<a href="book.jsp">选择图书</a><br>
<a href="shopcar.jsp">购物车</a><br>
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>输入姓名</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">
    -->

  </head>

  <body>
    <h3>请输入您的姓名:</h3>
    <form action="" mothed=post name="username">
    <input type="text" name="uname">
    <input type="submit" value="提交">
    </form>
    <%String name=request.getParameter("uname");
    if(name==null)
    name="";
    else
    session.setAttribute("uname",name);
     %>
  </body>
</html>

shopcar

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<a href="name.jsp">输入姓名</a><br>
<a href="book.jsp">选择图书</a><br>
<a href="shopcar.jsp">购物车</a><br>
</head>
<%! public String handleStr(String s){
***try{byte []bb=s.getBytes("gb2312");***//将此处gb2312更改为“iso-8859-1”
s=new String(bb);
}
catch(Exception exp){}
return s;
} %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>购物车</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">
    -->

  </head>

  <body>
   <%String personName=(String)session.getAttribute("uname");
   StringBuffer bookMess=null;
   if(personName==null||personName.length()==0){
   out.println("请到输入姓名页面输入姓名!");
   }
   else{
   bookMess=(StringBuffer)session.getAttribute("book");
   }
    %>
    <%
    String buyBook=new String(bookMess);
    double sum=0;
    String []price=buyBook.split("[^0123456789.]");
    if(price!=null){
    for(String item:price)
    try{
    sum+=Double.parseDouble(item);
    }
    catch(NumberFormatException exp){}
    }
     %><br>
     <% =handleStr(personName) %>购书信息:<br>
     <%=handleStr(buyBook) %><br>
     总价格:<%=sum%>
  </body>
</html>

name.jsp
book.jsp
shopcar

很奇怪,在购物车页面,购书信息出现乱码。检查代码:
在shopcar.jsp第12行:

需要修改的地方

修改完后:

修改完后的shopcar.jsp

错误原因:字符编码定义错误,为什么错,暂时不清楚?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值