您的位置:首页 > 编程语言 > Java开发

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">


不积跬步,无以至千里;不积小流,无以成江海!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: