zoukankan      html  css  js  c++  java
  • 练习模拟登录淘宝后记住用户名及购物车里的商品

    1.Login界面

    <%@page import="java.net.URLDecoder"%>
    <%@page import="jdk.nashorn.api.scripting.URLReader"%>
    <%@ 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>
    <%
    //提取cookie保存的卡号
    
    //获取所有cookie
    Cookie[]cks=request.getCookies();
    String username="";
    if(cks!=null){
    //遍历
    for(Cookie c:cks)
    {
        if(c.getName().equals("username"))
        {
            username = URLDecoder.decode(c.getValue());
        }
    }
    }
    %>
    <form action="Check.jsp" method="post">
    用户名:<input type="text" name="user" value="<%=username %>"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">
    </form>
    </body>
    </html>

    2.检查登录界面

    <%@page import="java.net.URLEncoder"%>
    <%@page import="com.haiqi.test.UserDAO"%>
    <%@ 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>
    <%
        String username=request.getParameter("user");//获取的值是与form表单中的文本框的name对应
        String password = request.getParameter("password");
        
        //数据验证
        if(username==null||password==null||
        username.equals("")||password.equals(""))
        {    //带缓存输出
            out.write("请正确登录");
            response.setHeader("refresh", "3;url=Login.jsp");
        }
        else
        {
            //检查登录信息
            UserDAO ud = new UserDAO();
            username = new String(request.getParameter("user").getBytes("ISO-8859-1"),"UTF-8");
            if(ud.CheckLogin(username, password))
            {
                
                //用cookie记住用户名
                Cookie cid = new Cookie("username",URLEncoder.encode(username));
                cid.setMaxAge(10*24*60*60);//设置过期时间
                response.addCookie(cid);
                //保持登录状态
                session.setAttribute("username", username);
                response.sendRedirect("Main.jsp");
                
                
            }
         else
            {
                out.write("登录失败!");
                //跳回登录界面
                response.setHeader("refresh", "2;URL=Login.jsp");
            }
        }
    %>
    </body>
    </html>

    3.主界面

    <%@page import="com.haiqi.test.UserDAO"%>
    <%@ 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>
    淘宝首页<br>
    <%
    Object obj =session.getAttribute("username");
    if(obj == null)
    {
        out.print("登录超时或未登录!");
        
        response.setHeader("refresh", "3;url=Login.jsp");
    }
    else
    {
        out.print(obj+",欢迎登录!");
    }
    %>
    <br>
    <br>
    <a href="Logout.jsp">退出登录</a>
    <form action="Main2.jsp" method="post">
    请输入你要购买的商品名称<input type="text" name="spname"><br>
    <input type="submit" value="加入购物车">
    
    
    <a href="Login.jsp">退出登录</a>
    </body>
    </html>

    4.退出界面

    <%@ 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>
    <%
    //销毁session
    session.invalidate();
    out.print("退出成功!");
    response.setHeader("refresh", "2;url=login.jsp");
    %>
    </body>
    </html>

    5.UserDAO

    package com.haiqi.test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    
    public class UserDAO {
        private ComboPooledDataSource cp = new ComboPooledDataSource("helloc3p0");
        public boolean CheckLogin(String username,String password)
        {
            boolean rtn = false;
            try {
                    Connection conn = cp.getConnection();
                    PreparedStatement ps = conn.prepareStatement("select * from account where a_name=?and a_password=?");
                    ps.setString(1, username);
                    ps.setString(2, password);
                    ResultSet rs = ps.executeQuery();
                    rtn=rs.next();
                } 
            catch (SQLException e) 
            {
                
                e.printStackTrace();
            }
            return rtn;
        }
    
    }

    6.添加商品页面

    <%@page import="java.net.URLEncoder"%>
    <%@page import="java.net.URLDecoder"%>
    <%@page import="jdk.nashorn.api.scripting.URLReader"%>
    <%@ 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>
     
    <%
    String spname=request.getParameter("spname");
    if(spname!=null)
    {
        spname=new String(spname.getBytes("ISO-8859-1"),"UTF-8");
    //设置cookie
        Cookie cok = new Cookie("spname", URLEncoder.encode(spname) );//利用cookie记住购物车内的物品
    //设置cookie的生命周期
        cok.setMaxAge(10*60*24);
    //发送cookie
        response.addCookie(cok);
    //跳转页面到菜单选择
        response.sendRedirect("thing.jsp");
    }
    %>
    
    <body>
    </body>
    
    </html>

    7.购物车页面

    <%@page import="java.net.URLDecoder"%>
    <%@page import="java.net.URLEncoder"%>
    <%@page import="jdk.nashorn.api.scripting.URLReader"%>
    <%@ 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>
    <!-- EL表达式 -->
    <!--name=${param.name };  -->
    添加到购物车的商品
    <br>
    <%
    
    
        Cookie []ck = request.getCookies();
        String spname="";
        if(ck!=null){
        for(Cookie c:ck)
        {   //遍历
            if(c.getName().equals("spname"))
            {
                spname=URLDecoder.decode(c.getValue());
            }
        }
            out.write(spname);
        }
        else
    {
        out.write("购物车没添加商品!");
    }
    
    %>
    <br>
    <a href="Main2.jsp">返回购物车</a>
    </body>
    </html>
  • 相关阅读:
    文件载入功能
    代码调试功能
    实用项
    连贯操作
    AR模式
    表名操作
    字段映射
    ThinkPHP中的模型二
    创建数据对象
    HDU 4888 Redraw Beautiful Drawings(最大流+判最大流网络是否唯一)
  • 原文地址:https://www.cnblogs.com/miracle-0807/p/6015600.html
Copyright © 2011-2022 走看看