赞
踩
源码分享在文末!
本项目为手机商城网站,实现了用户端和管理员、商家端。
在用户端:
在管理员商家端:
用户登录的过滤器
@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 } }
生成验证码
@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); } }
实现用户登录
@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); } }
用户注册
@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); } }
登录页面
注册页面
商城首页
用户登录之后页面
商品分类
商品详情页面
购物车页面
个人中心页面
订单页面
更改个人信息页面
管理员商家登录页面
后台管理首页
因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。