session的典型案例一:使用session实现购物车
程序员文章站
2024-03-19 22:22:58
...
使用session实现购物车
step1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>商品列表</title>
</head>
<body>
<h4>商品列表</h4>
<form action="<%=basePath%>servlet/step2" method="post">
<table border="1px">
<tr>
<td>商品名称</td>
<td>选择</td>
</tr>
<tr>
<td>java</td>
<td><input type="checkbox" name="book" value="java"></td>
</tr>
<tr>
<td>c</td>
<td><input type="checkbox" name="book" value="c"></td>
</tr>
<tr>
<td>c++</td>
<td><input type="checkbox" name="book" value="c++"></td>
</tr>
</table>
<input type="submit" value="确认">
</form>
</body>
</html>
step2.java:
package com.sa.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class step2 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
//1.获取选中的商品信息
String[] books=request.getParameterValues("book");
//2.将商品放入session
HttpSession session=request.getSession();
session.setAttribute("book", books);
//3.重定向到step2
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
response.sendRedirect(basePath+"/shopping/step2.jsp");
}
}
step2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'step1.jsp' starting page</title>
</head>
<body>
<h4>填写收件人,地址</h4>
<form action="<%=basePath%>servlet/step3" method="post">
收件人:<input type="text" name="receiver"><br>
地址:<input type="text" name="addr"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
step3.java:
package com.sa.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class step3 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String receiver=request.getParameter("receiver");
String addr=request.getParameter("addr");
HttpSession session=request.getSession();
session.setAttribute("receiver", receiver);
session.setAttribute("addr", addr);
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
response.sendRedirect(basePath+"/shopping/step3.jsp");
}
}
step3.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'step1.jsp' starting page</title>
</head>
<body>
<h4>信息总览</h4>
<form action="<%=basePath%>servlet/pay" method="post">
<%String[] books=(String[])session.getAttribute("book"); %>
商品:<%=Arrays.asList(books) %><br>
收件人:<%=session.getAttribute("receiver") %><br>
地址:<%=session.getAttribute("addr") %><br>
<input type="submit" value="付款">
</form>
</body>
</html>
测试:
推荐阅读
-
session的典型案例一:使用session实现购物车
-
详解PHP使用Redis存储session时的一个Warning定位
-
详解PHP使用Redis存储session时的一个Warning定位
-
ASP.NET中在一般处理程序中使用session的简单介绍
-
ASP.NET中在一般处理程序中使用session的简单介绍
-
ASP.NET中在一般处理程序中使用session的简单介绍
-
thinkPHP多域名情况下使用memcache方式共享session数据的实现方法
-
PHP中SESSION使用中的一点经验总结_PHP教程
-
如何使用Spring+redis实现对session的分布式管理
-
PHP使用Session遇到的一个Permission denied Notice解决办法_PHP