jsp ----session

来源:互联网 发布:壮游科技和塔人网络 编辑:程序博客网 时间:2024/05/16 01:35

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

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

0 0