javaWeb的session实现购物车
2016-03-01 22:46
477 查看
javaWeb的session实现购物车案例
- 1、先建立商品列表的jsp页面:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>session购物车应用案例</title> </head> <body> <h3>请选择购买商品</h3> <form action="<%=request.getContextPath()%>/product" method="post"> <table border="1" cellpadding="10" cellspacing="0"> <tr> <td>书名</td> <td>加入购物车</td> </tr> <tr> <td>Mysql</td> <td><input type="checkbox" name="book" value="Mysql" /></td> </tr> <tr> <td>sqlServer</td> <td><input type="checkbox" name="book" value="sqlServer" /></td> </tr> <tr> <td>mongodb</td> <td><input type="checkbox" name="book" value="mongodb" /></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>jquery</td> <td><input type="checkbox" name="book" value="jquery" /></td> </tr> <tr> <td>html</td> <td><input type="checkbox" name="book" value="html" /></td> </tr> <tr> <td><input type="submit" value="加入购物车" /></td> </tr> </table> </form> </body> </html>
- 2.建立一个servlet,用于session保存商品信息:
package session; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * 把数据存到sesssion * * @version 1.0 * @since JDK1.7 * @date 2016年3月1日 上午10:24:40 */ public class ProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //或取所有的值 String [] book=request.getParameterValues("book"); //把图书信息放到HttpSession中 request.getSession().setAttribute("book", book); //重定向(绝对路径) response.sendRedirect(request.getContextPath()+"/sessionByShoppingCart/userinfo.jsp"); } }
- 3.配置一下该ProductServlet
<servlet> <description></description> <servlet-name>ProductServlet</servlet-name> <servlet-class>session.ProductServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProductServlet</servlet-name> <url-pattern>/product</url-pattern> </servlet-mapping>
4.在建一个客户的收获基本信息的jsp:
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>请填写收货信息</h3> <form action="<%=request.getContextPath()%>/userinfo" method="post"> <table border="1" cellpadding="10" cellspacing="0"> <tr> <td colspan="2" align="center">收货基本信息</td> </tr> <tr> <td>收货人:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>收货地址:</td> <td><input type="text" name="address" /></td> </tr> <tr> <td>联系电话:</td> <td><input type="text" name="phone" /></td> </tr> <tr> <td colspan="2" align="center">支付信息</td> </tr> <tr> <td>支付类别:</td> <td><input type="radio" name="type" value="wangyin" />网银 <input type="radio" name="type" value="zhifubao"/>支付宝 </td> </tr> <tr> <td>账号:</td> <td><input type="text" name="account" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交" /></td> </tr> </table> </form> </body> </html>
5.一样的,也建立一个servlet实现信息的存储:
package session; import java.io.IOException; 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 UserinfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取参数 String name=request.getParameter("name"); String address=request.getParameter("address"); String phone=request.getParameter("phone"); String type=request.getParameter("type"); String account=request.getParameter("account"); //将参数放入session中 UserInfo user =new UserInfo(name, address, type, account,phone);//直接封装一个对象用来接收 HttpSession session=request.getSession(); session.setAttribute("name", user); //重定向 response.sendRedirect(request.getContextPath()+"/sessionByShoppingCart/pay.jsp"); } }
6.可以封装一个用户类来接收参数,便于存取:
package session; public class UserInfo { private String name; private String address; private String phone; private String type ; private String account; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } //带参的构造器==便于初始化 public UserInfo(String name, String address, String type, String account,String phone) { super(); this.name = name; this.address = address; this.type = type; this.account = account; this.phone=phone; } //无参的构造器==便于反射 public UserInfo(){} }
7.也给该UserinfoServlet配置一下
<servlet> <description></description> <servlet-name>userinfoServlet</servlet-name> <servlet-class>session.UserinfoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>userinfoServlet</servlet-name> <url-pattern>/userinfo</url-pattern> </servlet-mapping>
8.建立一个确认订单信息的jsp页面,把session存储的东西进行展示:
<%@page import="session.UserInfo"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h3>请确认购买信息</h3> <% UserInfo user=(UserInfo)session.getAttribute("name"); String [] books=(String[])session.getAttribute("book"); %> <form action=""> <table border="1" cellpadding="10" cellspacing="0"> <tr> <td colspan="2" align="center">收货信息</td> </tr> <tr> <td>收货人:</td> <td><input type="text" name="name" value="<%=user.getName()%>" /></td> </tr> <tr> <td>收货地址:</td> <td><input type="text" name="address" value="<%=user.getAddress() %>" /></td> </tr> <tr> <td>联系电话:</td> <td><input type="text" name="phone" value="<%=user.getPhone() %>" /></td> </tr> <tr> <td colspan="2" align="center">支付信息</td> </tr> <tr> <td>支付类别:</td> <td><input name="type" value="<%=user.getType() %>" /> </td> </tr> <tr> <td>账号:</td> <td><input type="text" name="account" value="<%=user.getAccount() %>" /></td> </tr> <tr> <td colspan="2" align="center">商品信息</td> </tr> <tr> <td> <%for(String book:books){ out.println(book); out.println("<br>"); } %> </td> </tr> <tr> <td colspan="2"><input type="submit" value="确认" /></td> </tr> </table> </form> </body> </html>
需要注意的是:form表单提交时,要用绝对路径,还有servlet重定向时,也要用绝对路径,如:
1、
response.sendRedirect(request.getContextPath()+"/sessionByShoppingCart/userinfo.jsp");
2、
<form action="<%=request.getContextPath()%>/userinfo" method="post">
不积跬步,无以至千里;不积小流,无以成江海!
相关文章推荐
- Java多线程之Executor框架(2)
- Spring连接两个以上数据库
- struts(tags)
- Eclipse 使用 maven 构建demo
- struts(result)
- Struts(ognl)
- struts(一)
- STS: Spring Boot and MyBatis
- Spring学习笔记2016-3-1
- Java 集合类详解
- spring 中的@Resource
- java基础篇——异常处理throw与throws的区别
- MyEclipse使用总结——设置MyEclipse开发项目时使用的JDK
- java单例模式
- MyEclipse使用总结——MyEclipse文件查找技巧 ctrl+shift+R ctrl+H
- MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
- MyEclipse使用总结——MyEclipse10安装SVN插件
- 配置struts2的默认后缀
- AnnotationTransactionAttributeSource is only available on Java 1.5 and highe
- Java中日期的格式化