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

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<br>JSP Login.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Login.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><form action=Login.jsp method="POST" ><br>Login Name: <input type="text" name="Name"><br><br>Login Password: <input type="text" name="Password" ><br><br><input type="submit" value="Send"><br><br><form><br><br><% if (request.getParameter("Name") != null &&<br> request.getParameter("Password") != null) { <br>String Name = request.getParameter("Name");<br>String Password = request.getParameter("Password");<br><br>if (Name.equals("mike") && Password.equals("1234")) { <br>session.setAttribute("Login", "OK");<br>response.sendRedirect("Member.jsp");<br>}<br>else { <br>out.println("登录错误,输入正确名称"); <br>} <br>}<br>%><br><br></body><br></html> <br><br><br>JSP Member.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Member.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><% <br>String Login = (String)session.getAttribute("Login");<br><br>if (Login != null && Login.equals("OK")) { <br>out.println("欢迎进入");<br>session.invalidate(); <br>} <br>else { <br>out.println("先登录,谢谢") ;<br>out.println("<br>经过五秒之后,网页会自动返回Login.jsp");<br><br>response.setHeader("Refresh","5;URL=Login.jsp"); <br>}<br>%><br><br></body><br></html> <br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值