cookieJsp购物车

举报
小傅哥 发表于 2021/04/23 01:20:40 2021/04/23
【摘要】 源码下载 用到技术 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


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>Cookie购物车</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="购物、cookie">
  15. <meta http-equiv="description" content="cookie购物车">
  16. </head>
  17. <body>
  18. <div style="color: red;">*测试用户:admin 密码:admin</div>
  19. <form action="CookieShopCar?type=1" method="post">
  20. <table border="1">
  21. <tr>
  22. <th colspan="2" align="center">
  23. 欢迎登陆惊涛购物
  24. </th>
  25. </tr>
  26. <tr>
  27. <td>用户名:</td>
  28. <td><input type="text" id="uname" name="uname"/></td>
  29. </tr>
  30. <tr>
  31. <td>密码:</td>
  32. <td><input type="password" id="upwd" name="upwd"/></td>
  33. </tr>
  34. <tr>
  35. <td colspan="2" align="center">
  36. <input type="submit" value="登陆"/>
  37. <span>${ msg }</span>
  38. </td>
  39. </tr>
  40. </table>
  41. </form>
  42. </body>
  43. </html>


shopWelcome.jsp


  
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>Cookie购物车</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="购物、cookie">
  16. <meta http-equiv="description" content="cookie购物车">
  17. <script src="js/jquery-1.4.2.js" type="text/javascript" language="javascript"></script>
  18. <script type="text/javascript" language="javascript">
  19. $(function(){
  20. $("tr td a").click(function(){
  21. $.post("CookieShopCar",
  22. { type: "2" ,
  23. sid:$(this).next().val()
  24. },
  25. function(data){
  26. var obj = eval(data);
  27. var buys = "";
  28. for(i in obj){
  29. buys += obj[i].name+","+obj[i].price+"\r\n";
  30. }
  31. $("#scar").val(buys);
  32. });
  33. });
  34. });
  35. </script>
  36. </head>
  37. <body>
  38. <div>
  39. 欢迎<span style="color: red;">${ uName }</span>来到惊涛骇浪购物网
  40. </div>
  41. <table border="1">
  42. <tr>
  43. <th colspan="4" align="center">货物列表</th>
  44. </tr>
  45. <tr>
  46. <th>序号</th>
  47. <th>商品</th>
  48. <th>价格</th>
  49. <th>操作</th>
  50. </tr>
  51. <c:forEach items="${ shops }" var="sp" varStatus="id">
  52. <tr>
  53. <td>${ id.index + 1 }</td>
  54. <td>${ sp.sname }</td>
  55. <td>${ sp.sprice }元</td>
  56. <td>
  57. <a href="javascript:void(0)" id="buyShop" >购买</a>
  58. <input type="hidden" value="${ sp.sid }"/>
  59. </td>
  60. </tr>
  61. </c:forEach>
  62. </table>
  63. <div style="color: blue;font-weight: bolder;">
  64. 我的购物车
  65. </div>
  66. <textarea rows="50" cols="20" id="scar"></textarea>
  67. </body>
  68. </html>


ShopBeanShopBean.java


  
  1. package com.soft.etc.shop.bean;
  2. public class ShopBean {
  3. private String sid; //商品id
  4. private String sname; //商品名字
  5. private String sprice; //商品价格
  6. public ShopBean() {
  7. super();
  8. }
  9. public ShopBean(String sid, String sname, String sprice) {
  10. super();
  11. this.sid = sid;
  12. this.sname = sname;
  13. this.sprice = sprice;
  14. }
  15. public String getSid() {
  16. return sid;
  17. }
  18. public void setSid(String sid) {
  19. this.sid = sid;
  20. }
  21. public String getSname() {
  22. return sname;
  23. }
  24. public void setSname(String sname) {
  25. this.sname = sname;
  26. }
  27. public String getSprice() {
  28. return sprice;
  29. }
  30. public void setSprice(String sprice) {
  31. this.sprice = sprice;
  32. }
  33. }


UserBeanUserBean.java


  
  1. package com.soft.etc.shop.bean;
  2. public class UserBean {
  3. private String userName;
  4. private String userPwd;
  5. public UserBean() {
  6. super();
  7. }
  8. public UserBean(String userName, String userPwd) {
  9. super();
  10. this.userName = userName;
  11. this.userPwd = userPwd;
  12. }
  13. public String getUserName() {
  14. return userName;
  15. }
  16. public void setUserName(String userName) {
  17. this.userName = userName;
  18. }
  19. public String getUserPwd() {
  20. return userPwd;
  21. }
  22. public void setUserPwd(String userPwd) {
  23. this.userPwd = userPwd;
  24. }
  25. }


ShopDataShopDataShopData.java


  
  1. package com.soft.etc.shop.data;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.soft.etc.shop.bean.ShopBean;
  5. public class ShopData {
  6. private List<ShopBean> shops;
  7. public List<ShopBean> getShops() {
  8. shops = new ArrayList<ShopBean>();
  9. ShopBean sb_1 = new ShopBean();
  10. sb_1.setSid("1");
  11. sb_1.setSname("ipad");
  12. sb_1.setSprice("3200");
  13. shops.add(sb_1);
  14. ShopBean sb_2 = new ShopBean();
  15. sb_2.setSid("2");
  16. sb_2.setSname("Nokia 920T");
  17. sb_2.setSprice("4850");
  18. shops.add(sb_2);
  19. ShopBean sb_3 = new ShopBean();
  20. sb_3.setSid("3");
  21. sb_3.setSname("thinkpad T420i");
  22. sb_3.setSprice("6999");
  23. shops.add(sb_3);
  24. ShopBean sb_4 = new ShopBean();
  25. sb_4.setSid("4");
  26. sb_4.setSname("佳能 单反");
  27. sb_4.setSprice("4350");
  28. shops.add(sb_4);
  29. ShopBean sb_5 = new ShopBean();
  30. sb_5.setSid("5");
  31. sb_5.setSname("WD 1TB 移动硬盘");
  32. sb_5.setSprice("499");
  33. shops.add(sb_5);
  34. ShopBean sb_6 = new ShopBean();
  35. sb_6.setSid("6");
  36. sb_6.setSname("雅马哈 摩托车");
  37. sb_6.setSprice("7900");
  38. shops.add(sb_6);
  39. ShopBean sb_7 = new ShopBean();
  40. sb_7.setSid("7");
  41. sb_7.setSname("无线鼠标lenovo");
  42. sb_7.setSprice("70");
  43. shops.add(sb_7);
  44. ShopBean sb_8 = new ShopBean();
  45. sb_8.setSid("8");
  46. sb_8.setSname("笔记本超强散热架");
  47. sb_8.setSprice("66");
  48. shops.add(sb_8);
  49. ShopBean sb_9 = new ShopBean();
  50. sb_9.setSid("9");
  51. sb_9.setSname("原装电脑包");
  52. sb_9.setSprice("120");
  53. shops.add(sb_9);
  54. ShopBean sb_10 = new ShopBean();
  55. sb_10.setSid("10");
  56. sb_10.setSname("500G笔记本硬盘");
  57. sb_10.setSprice("350");
  58. shops.add(sb_10);
  59. return shops;
  60. }
  61. public void setShops(List<ShopBean> shops) {
  62. this.shops = shops;
  63. }
  64. }


UserDataUserData.java


  
  1. package com.soft.etc.shop.data;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.soft.etc.shop.bean.UserBean;
  5. public class UserData {
  6. private List<UserBean> users;
  7. public List<UserBean> getUsers() {
  8. users = new ArrayList<UserBean>();
  9. UserBean user_1 = new UserBean();
  10. user_1.setUserName("admin");
  11. user_1.setUserPwd("admin");
  12. users.add(user_1);
  13. UserBean user_2 = new UserBean();
  14. user_2.setUserName("fuzhengwei");
  15. user_2.setUserPwd("123");
  16. users.add(user_2);
  17. return users;
  18. }
  19. public void setUsers(List<UserBean> users) {
  20. this.users = users;
  21. }
  22. }


MyFilterMyFilter.java


  
  1. package com.soft.etc.shop.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. public class MyFilter implements Filter {
  10. private String encoding = null;
  11. private FilterConfig filterConfig = null;
  12. public void destroy() {
  13. this.encoding = null;
  14. this.filterConfig = null;
  15. }
  16. public void doFilter(ServletRequest servletReq,ServletResponse servletRes,
  17. FilterChain chain) throws IOException, ServletException {
  18. if(filterConfig!=null){
  19. servletReq.setCharacterEncoding(encoding);
  20. servletRes.setContentType("text/html;charset="+encoding);
  21. }
  22. chain.doFilter(servletReq, servletRes);
  23. }
  24. public void init(FilterConfig filterConfig) throws ServletException {
  25. this.filterConfig = filterConfig;
  26. this.encoding = filterConfig.getInitParameter("encoding");
  27. }
  28. }


ShopServiceShopService.java


  
  1. package com.soft.etc.shop.service;
  2. import java.util.List;
  3. import com.soft.etc.shop.bean.ShopBean;
  4. public class ShopService {
  5. /**
  6. * 获得用户购买的商品
  7. * @param shops
  8. * @param sid
  9. * @return
  10. */
  11. public ShopBean getShopMsg(List<ShopBean> shops, String sid) {
  12. ShopBean shopBean = null;
  13. for(ShopBean s:shops){
  14. if(s.getSid().equals(sid.trim())){
  15. shopBean = s;
  16. }
  17. }
  18. return shopBean;
  19. }
  20. }


UserServiceUserService.java


  
  1. package com.soft.etc.shop.service;
  2. import java.util.List;
  3. import com.soft.etc.shop.bean.UserBean;
  4. public class UserService {
  5. /**
  6. * 验证登陆
  7. * @param user
  8. * @param userName
  9. * @param userPwd
  10. * @return
  11. */
  12. public boolean userLogin(List<UserBean> user, String userName, String userPwd) {
  13. boolean isLogin = false;
  14. for(UserBean u:user){
  15. if(u.getUserName().equals(userName.trim()) && u.getUserPwd().equals(userPwd.trim())){
  16. isLogin = true;
  17. }
  18. }
  19. return isLogin;
  20. }
  21. }


CookieShopCarCookieShopCar.java


  
  1. package com.soft.etc.shop.service;
  2. import java.util.List;
  3. import com.soft.etc.shop.bean.UserBean;
  4. public class UserService {
  5. /**
  6. * 验证登陆
  7. * @param user
  8. * @param userName
  9. * @param userPwd
  10. * @return
  11. */
  12. public boolean userLogin(List<UserBean> user, String userName, String userPwd) {
  13. boolean isLogin = false;
  14. for(UserBean u:user){
  15. if(u.getUserName().equals(userName.trim()) && u.getUserPwd().equals(userPwd.trim())){
  16. isLogin = true;
  17. }
  18. }
  19. return isLogin;
  20. }
  21. }


Web.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <servlet>
  8. <servlet-name>CookieShopCar</servlet-name>
  9. <servlet-class>com.soft.etc.shop.servlet.CookieShopCar</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>CookieShopCar</servlet-name>
  13. <url-pattern>/CookieShopCar</url-pattern>
  14. </servlet-mapping>
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. <filter>
  19. <filter-name>MyFilter</filter-name>
  20. <filter-class>com.soft.etc.shop.filter.MyFilter</filter-class>
  21. <init-param>
  22. <param-name>encoding</param-name>
  23. <param-value>GBK</param-value>
  24. </init-param>
  25. </filter>
  26. <filter-mapping>
  27. <filter-name>MyFilter</filter-name>
  28. <url-pattern>/*</url-pattern>
  29. <dispatcher>FORWARD</dispatcher>
  30. <dispatcher>REQUEST</dispatcher>
  31. </filter-mapping>
  32. </web-app>


 

 

 

文章来源: bugstack.blog.csdn.net,作者:Yao__Shun__Yu,版权归原作者所有,如需转载,请联系作者。

原文链接:bugstack.blog.csdn.net/article/details/8713572

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。