cookieJsp购物车
【摘要】 源码下载
用到技术
cookie、jqAjax、el+jstl
包结构
代码
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = reque...
用到技术
cookie、jqAjax、el+jstl
包结构
代码
index.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>Cookie购物车</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="购物、cookie">
-
<meta http-equiv="description" content="cookie购物车">
-
</head>
-
-
<body>
-
<div style="color: red;">*测试用户:admin 密码:admin</div>
-
<form action="CookieShopCar?type=1" method="post">
-
<table border="1">
-
<tr>
-
<th colspan="2" align="center">
-
欢迎登陆惊涛购物
-
</th>
-
</tr>
-
<tr>
-
<td>用户名:</td>
-
<td><input type="text" id="uname" name="uname"/></td>
-
</tr>
-
<tr>
-
<td>密码:</td>
-
<td><input type="password" id="upwd" name="upwd"/></td>
-
</tr>
-
<tr>
-
<td colspan="2" align="center">
-
<input type="submit" value="登陆"/>
-
<span>${ msg }</span>
-
</td>
-
</tr>
-
</table>
-
</form>
-
-
</body>
-
</html>
shopWelcome.jsp
-
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
-
<%
-
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>Cookie购物车</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="购物、cookie">
-
<meta http-equiv="description" content="cookie购物车">
-
<script src="js/jquery-1.4.2.js" type="text/javascript" language="javascript"></script>
-
<script type="text/javascript" language="javascript">
-
-
$(function(){
-
-
$("tr td a").click(function(){
-
-
$.post("CookieShopCar",
-
{ type: "2" ,
-
sid:$(this).next().val()
-
},
-
-
function(data){
-
-
var obj = eval(data);
-
var buys = "";
-
for(i in obj){
-
buys += obj[i].name+","+obj[i].price+"\r\n";
-
-
}
-
$("#scar").val(buys);
-
});
-
-
});
-
-
});
-
-
</script>
-
-
</head>
-
-
<body>
-
<div>
-
欢迎<span style="color: red;">${ uName }</span>来到惊涛骇浪购物网
-
</div>
-
<table border="1">
-
<tr>
-
<th colspan="4" align="center">货物列表</th>
-
</tr>
-
<tr>
-
<th>序号</th>
-
<th>商品</th>
-
<th>价格</th>
-
<th>操作</th>
-
</tr>
-
<c:forEach items="${ shops }" var="sp" varStatus="id">
-
<tr>
-
<td>${ id.index + 1 }</td>
-
<td>${ sp.sname }</td>
-
<td>${ sp.sprice }元</td>
-
<td>
-
<a href="javascript:void(0)" id="buyShop" >购买</a>
-
<input type="hidden" value="${ sp.sid }"/>
-
</td>
-
</tr>
-
</c:forEach>
-
</table>
-
<div style="color: blue;font-weight: bolder;">
-
我的购物车
-
</div>
-
<textarea rows="50" cols="20" id="scar"></textarea>
-
</body>
-
</html>
ShopBeanShopBean.java
-
package com.soft.etc.shop.bean;
-
-
public class ShopBean {
-
-
private String sid; //商品id
-
private String sname; //商品名字
-
private String sprice; //商品价格
-
-
public ShopBean() {
-
super();
-
}
-
-
public ShopBean(String sid, String sname, String sprice) {
-
super();
-
this.sid = sid;
-
this.sname = sname;
-
this.sprice = sprice;
-
}
-
-
public String getSid() {
-
return sid;
-
}
-
public void setSid(String sid) {
-
this.sid = sid;
-
}
-
public String getSname() {
-
return sname;
-
}
-
public void setSname(String sname) {
-
this.sname = sname;
-
}
-
public String getSprice() {
-
return sprice;
-
}
-
public void setSprice(String sprice) {
-
this.sprice = sprice;
-
}
-
-
}
UserBeanUserBean.java
-
package com.soft.etc.shop.bean;
-
-
public class UserBean {
-
-
private String userName;
-
private String userPwd;
-
-
public UserBean() {
-
super();
-
}
-
-
public UserBean(String userName, String userPwd) {
-
super();
-
this.userName = userName;
-
this.userPwd = userPwd;
-
}
-
-
public String getUserName() {
-
return userName;
-
}
-
public void setUserName(String userName) {
-
this.userName = userName;
-
}
-
public String getUserPwd() {
-
return userPwd;
-
}
-
public void setUserPwd(String userPwd) {
-
this.userPwd = userPwd;
-
}
-
-
}
ShopDataShopDataShopData.java
-
package com.soft.etc.shop.data;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.soft.etc.shop.bean.ShopBean;
-
-
public class ShopData {
-
-
private List<ShopBean> shops;
-
-
public List<ShopBean> getShops() {
-
-
shops = new ArrayList<ShopBean>();
-
-
ShopBean sb_1 = new ShopBean();
-
sb_1.setSid("1");
-
sb_1.setSname("ipad");
-
sb_1.setSprice("3200");
-
shops.add(sb_1);
-
-
ShopBean sb_2 = new ShopBean();
-
sb_2.setSid("2");
-
sb_2.setSname("Nokia 920T");
-
sb_2.setSprice("4850");
-
shops.add(sb_2);
-
-
ShopBean sb_3 = new ShopBean();
-
sb_3.setSid("3");
-
sb_3.setSname("thinkpad T420i");
-
sb_3.setSprice("6999");
-
shops.add(sb_3);
-
-
ShopBean sb_4 = new ShopBean();
-
sb_4.setSid("4");
-
sb_4.setSname("佳能 单反");
-
sb_4.setSprice("4350");
-
shops.add(sb_4);
-
-
ShopBean sb_5 = new ShopBean();
-
sb_5.setSid("5");
-
sb_5.setSname("WD 1TB 移动硬盘");
-
sb_5.setSprice("499");
-
shops.add(sb_5);
-
-
ShopBean sb_6 = new ShopBean();
-
sb_6.setSid("6");
-
sb_6.setSname("雅马哈 摩托车");
-
sb_6.setSprice("7900");
-
shops.add(sb_6);
-
-
ShopBean sb_7 = new ShopBean();
-
sb_7.setSid("7");
-
sb_7.setSname("无线鼠标lenovo");
-
sb_7.setSprice("70");
-
shops.add(sb_7);
-
-
ShopBean sb_8 = new ShopBean();
-
sb_8.setSid("8");
-
sb_8.setSname("笔记本超强散热架");
-
sb_8.setSprice("66");
-
shops.add(sb_8);
-
-
ShopBean sb_9 = new ShopBean();
-
sb_9.setSid("9");
-
sb_9.setSname("原装电脑包");
-
sb_9.setSprice("120");
-
shops.add(sb_9);
-
-
ShopBean sb_10 = new ShopBean();
-
sb_10.setSid("10");
-
sb_10.setSname("500G笔记本硬盘");
-
sb_10.setSprice("350");
-
shops.add(sb_10);
-
-
return shops;
-
}
-
-
public void setShops(List<ShopBean> shops) {
-
this.shops = shops;
-
}
-
-
}
UserDataUserData.java
-
package com.soft.etc.shop.data;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.soft.etc.shop.bean.UserBean;
-
-
public class UserData {
-
-
private List<UserBean> users;
-
-
public List<UserBean> getUsers() {
-
-
users = new ArrayList<UserBean>();
-
-
UserBean user_1 = new UserBean();
-
user_1.setUserName("admin");
-
user_1.setUserPwd("admin");
-
users.add(user_1);
-
-
UserBean user_2 = new UserBean();
-
user_2.setUserName("fuzhengwei");
-
user_2.setUserPwd("123");
-
users.add(user_2);
-
-
return users;
-
}
-
-
public void setUsers(List<UserBean> users) {
-
this.users = users;
-
}
-
-
}
MyFilterMyFilter.java
-
package com.soft.etc.shop.filter;
-
-
import java.io.IOException;
-
-
import javax.servlet.Filter;
-
import javax.servlet.FilterChain;
-
import javax.servlet.FilterConfig;
-
import javax.servlet.ServletException;
-
import javax.servlet.ServletRequest;
-
import javax.servlet.ServletResponse;
-
-
public class MyFilter implements Filter {
-
-
private String encoding = null;
-
private FilterConfig filterConfig = null;
-
-
public void destroy() {
-
this.encoding = null;
-
this.filterConfig = null;
-
}
-
-
public void doFilter(ServletRequest servletReq,ServletResponse servletRes,
-
FilterChain chain) throws IOException, ServletException {
-
if(filterConfig!=null){
-
servletReq.setCharacterEncoding(encoding);
-
servletRes.setContentType("text/html;charset="+encoding);
-
}
-
chain.doFilter(servletReq, servletRes);
-
}
-
-
public void init(FilterConfig filterConfig) throws ServletException {
-
this.filterConfig = filterConfig;
-
this.encoding = filterConfig.getInitParameter("encoding");
-
}
-
-
-
}
ShopServiceShopService.java
-
package com.soft.etc.shop.service;
-
-
import java.util.List;
-
-
import com.soft.etc.shop.bean.ShopBean;
-
-
public class ShopService {
-
-
/**
-
* 获得用户购买的商品
-
* @param shops
-
* @param sid
-
* @return
-
*/
-
public ShopBean getShopMsg(List<ShopBean> shops, String sid) {
-
ShopBean shopBean = null;
-
for(ShopBean s:shops){
-
if(s.getSid().equals(sid.trim())){
-
shopBean = s;
-
}
-
}
-
return shopBean;
-
}
-
-
}
UserServiceUserService.java
-
package com.soft.etc.shop.service;
-
-
import java.util.List;
-
-
import com.soft.etc.shop.bean.UserBean;
-
-
public class UserService {
-
-
/**
-
* 验证登陆
-
* @param user
-
* @param userName
-
* @param userPwd
-
* @return
-
*/
-
public boolean userLogin(List<UserBean> user, String userName, String userPwd) {
-
-
boolean isLogin = false;
-
for(UserBean u:user){
-
if(u.getUserName().equals(userName.trim()) && u.getUserPwd().equals(userPwd.trim())){
-
isLogin = true;
-
}
-
}
-
return isLogin;
-
}
-
-
}
CookieShopCarCookieShopCar.java
-
package com.soft.etc.shop.service;
-
-
import java.util.List;
-
-
import com.soft.etc.shop.bean.UserBean;
-
-
public class UserService {
-
-
/**
-
* 验证登陆
-
* @param user
-
* @param userName
-
* @param userPwd
-
* @return
-
*/
-
public boolean userLogin(List<UserBean> user, String userName, String userPwd) {
-
-
boolean isLogin = false;
-
for(UserBean u:user){
-
if(u.getUserName().equals(userName.trim()) && u.getUserPwd().equals(userPwd.trim())){
-
isLogin = true;
-
}
-
}
-
return isLogin;
-
}
-
-
}
Web.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5"
-
xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
<servlet>
-
<servlet-name>CookieShopCar</servlet-name>
-
<servlet-class>com.soft.etc.shop.servlet.CookieShopCar</servlet-class>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>CookieShopCar</servlet-name>
-
<url-pattern>/CookieShopCar</url-pattern>
-
</servlet-mapping>
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
-
<filter>
-
<filter-name>MyFilter</filter-name>
-
<filter-class>com.soft.etc.shop.filter.MyFilter</filter-class>
-
<init-param>
-
<param-name>encoding</param-name>
-
<param-value>GBK</param-value>
-
</init-param>
-
</filter>
-
-
<filter-mapping>
-
<filter-name>MyFilter</filter-name>
-
<url-pattern>/*</url-pattern>
-
<dispatcher>FORWARD</dispatcher>
-
<dispatcher>REQUEST</dispatcher>
-
</filter-mapping>
-
</web-app>
文章来源: bugstack.blog.csdn.net,作者:Yao__Shun__Yu,版权归原作者所有,如需转载,请联系作者。
原文链接:bugstack.blog.csdn.net/article/details/8713572
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)