当前位置:   article > 正文

Java课程设计:基于Javaweb的手机商城网站(内附源码)_javaweb网上商城源码

javaweb网上商城源码

源码分享在文末!


一、项目介绍

本项目为手机商城网站,实现了用户端和管理员、商家端。

在用户端:

  1. 实现了注册登录功能;
  2. 实现了对商品的选购功能;
  3. 用户可以把对应商品加入购物车;
  4. 用户在购买商品后自动生成订单;
  5. 用户修改自己收货地址;
  6. 用户修改自己个人信息;
  7. 对商品进行分类显示;

在管理员商家端:

  1. 实现了登录注册功能;
  2. 实现了对用户的管理;
  3. 实现了对手机的分类管理;
  4. 实现了对手机商品的管理;
  5. 实现了对手机商品订单的管理;

二、项目技术栈

  • 后端:javaweb+jdk+tomcat8+mvc
  • 前端:jsp+css+js
  • 数据库:mysql

三、核心代码

用户登录的过滤器

@WebFilter("/adminTest/*")
public class AdminLogin implements Filter {

    public AdminLogin() {
    }

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		
		req.setCharacterEncoding("UTF-8");
		String url=req.getRequestURI();

		if(url.indexOf(".css")>0) {
			chain.doFilter(req, resp);
			return;
		}else {
			response.setContentType("text/html; charset=UTF-8");
		}


		
		HttpSession session = req.getSession();
		
		String flag = (String)session.getAttribute("isLogin");
		
		String request_uri = req.getRequestURI();
		String ctxPath= req.getContextPath();
		String uri= request_uri.substring(ctxPath.length());
		
	//	System.out.println(request_uri+"##"+ctxPath+"##"+uri+"##"+flag);
		
		if(uri.contains("admin_")){
			if(flag !=null && flag.equals("2")) {
				chain.doFilter(req, resp);
			}else{
				PrintWriter out = resp.getWriter();
				out.write("<script>");
				out.write("alert('请先登录!');");
				out.write("location.href='login.jsp';");
				out.write("</script>");
				out.close();
				return;
			}
		}else{
			chain.doFilter(req, resp);	
		}
		
		return;
		
		 //不通过则直接return
		// pass the request along the filter chain
	//	chain.doFilter(req, resp); // 通过则使用这条语句
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

生成验证码

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		//服务器通知浏览器不要缓存
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		response.setHeader("expires","0");
		
		//在内存中创建一个长80,宽30的图片,默认黑色背景
		//参数一:长
		//参数二:宽
		//参数三:颜色
		int width = 136;
		int height = 38;
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		
		//获取画笔
		Graphics g = image.getGraphics();
		//设置画笔颜色为灰色
		g.setColor(Color.GRAY);
		//填充图片
		g.fillRect(0,0, width,height);
		
		//产生4个随机验证码,12Ey
		String checkCode = getCheckCode();
		//将验证码放入HttpSession中
		request.getSession().setAttribute("CHECKCODE_SERVER",checkCode);

		//设置画笔颜色为黄色
		g.setColor(Color.YELLOW);
		//设置字体的小大
		g.setFont(new Font("黑体",Font.BOLD,30));
		//向图片上写入验证码
		g.drawString(checkCode,36,28);

		//将内存中的图片输出到浏览器
		//参数一:图片对象
		//参数二:图片的格式,如PNG,JPG,GIF
		//参数三:图片输出到哪里去
		ImageIO.write(image,"PNG",response.getOutputStream());
	}
	/**
	 * 产生4位随机字符串 
	 */
	private String getCheckCode() {
		String base = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
		int size = base.length();
		Random r = new Random();
		StringBuffer sb = new StringBuffer();
		for(int i=1;i<=4;i++){
			//产生0到size-1的随机值
			int index = r.nextInt(size);
			//在base字符串中获取下标为index的字符
			char c = base.charAt(index);
			//将c放入到StringBuffer中去
			sb.append(c);
		}
		return sb.toString();
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request,response);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

实现用户登录

@WebServlet("/loginAdminServlet")
public class LoginAdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        UserService service = new UserServiceImpl();

        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");

        User user = service.loginByNameAndPassword(userName, passWord);

        if(user != null && "2".equals(user.getUser_status())) {
            HttpSession session = request.getSession();

            session.setAttribute("admin", user);
            session.setAttribute("isLogin", "2");

            response.sendRedirect(request.getContextPath()+"/admin_index.jsp");
        }else{
            PrintWriter out = response.getWriter();

            out.write("<script>");
            out.write("alert('管理员登录失败!');");
            out.write("location.href='admin_login.jsp'");
            out.write("</script>");
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

用户注册

@WebServlet("/regServlet")
public class RegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        UserService service = new UserServiceImpl();

        String userName = request.getParameter("userName").trim();
        String userPassword = request.getParameter("userPassword").trim();
        String enUserPassword = request.getParameter("enUserPassword".trim());
        String verifycode = request.getParameter("verifycode".trim());

        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");//确保验证一次性
        if (!verifycode.equalsIgnoreCase(checkcode_server)){
            request.setAttribute("msg","验证码错误");
            request.setAttribute("userName",userName);
            request.setAttribute("userPassword",userPassword);
            request.setAttribute("enUserPassword",enUserPassword);

            request.getRequestDispatcher("reg.jsp").forward(request,response);
            return;
        }else {
            if (!userPassword.equals(enUserPassword)) {
                request.setAttribute("msg","密码确认错误");
                request.setAttribute("userName",userName);
                request.setAttribute("userPassword",userPassword);
                request.setAttribute("enUserPassword",enUserPassword);
                request.getRequestDispatcher("reg.jsp").forward(request,response);
                return;
            }else {
                User user = service.findUserByUserName(userName);

                if (user != null) {
                    request.setAttribute("msg", "用户已存在");
                    request.setAttribute("userName", userName);
                    request.setAttribute("userPassword", userPassword);
                    request.setAttribute("enUserPassword", enUserPassword);
                    request.getRequestDispatcher("reg.jsp").forward(request, response);
                    return;
                } else {
                    User u = new User(userName,"无昵称哦",userPassword,null,"0","0","1","default_0.jpg");
                    service.addUser(u);

                    request.setAttribute("msg", "注册成功!");
                    request.getRequestDispatcher("reg.jsp").forward(request,response);
                }
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

四、项目展示

登录页面
在这里插入图片描述

注册页面
在这里插入图片描述

商城首页
在这里插入图片描述

用户登录之后页面

在这里插入图片描述

商品分类
在这里插入图片描述

商品详情页面

在这里插入图片描述

购物车页面
在这里插入图片描述

个人中心页面
在这里插入图片描述

订单页面

在这里插入图片描述

更改个人信息页面
在这里插入图片描述

管理员商家登录页面

在这里插入图片描述
后台管理首页

在这里插入图片描述

五、源码获取

因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!

点我获取源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/816976
推荐阅读
相关标签
  

闽ICP备14008679号