使用CooKie实现数据缓存至客户端

对cookie的使用测试

登录界面

<%@ 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>学生信息管理系统--用户登录</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
    <%
        //判断是否选择记住密码
        boolean isPwd = false;
        //获取cookie对象
        Cookie[] cs = request.getCookies();
        if (cs != null) {
            for (Cookie c : cs) {
                if (c.getName().equals("userName")) {
                    String userName = c.getValue();
                    pageContext.setAttribute("userName", userName);
                }

                if (c.getName().equals("pwd")) {
                    String pwd = c.getValue();
                    pageContext.setAttribute("pwd", pwd);
                }
                if (c.getName().equals("isPwd")) {
                    isPwd = true;
                }
            }
        }
    %>
    <h1 align="center">用户登录</h1>
    <hr>

    <form action="m_login.jsp" method="post">

        <p>
            用户名:<input type="text" name="userName" value="${pageScope.userName}">
        </p>
        <p>
            密码:<input type="password" name="pwd" value="${pageScope.pwd}">
        </p>
        <p>
            <%
                if(isPwd){
                    out.print("<input type='checkbox' name='isPwd' checked>");
                }else{
                    out.print("<input type='checkbox' name='isPwd'>");
                }

             %>
            记住密码
        </p>
        <p>
            <input type="submit" value="登录">&nbsp;&nbsp;<a href="zhuce.jsp">注册用户</a>
        </p>

    </form>
    <font color="red">${mess}</font>
</body>
</html>

登录中转界面

<%@ page language="java"
    import="java.util.*,com.lyb.service.*,com.lyb.ShiTiLei.*"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<%
    //设置请求参数编码
    request.setCharacterEncoding("utf-8");

    UserService us = new UserService();

    //接受index.jsp传过来的参数
    String userName = request.getParameter("userName");
    String pwd = request.getParameter("pwd");
    String isPwd = request.getParameter("isPwd");
    //判断参数是否为空
    if ((userName == null || userName.equals("")|| (pwd==null || pwd.equals("")))
            || (pwd == null || pwd.equals(""))) {
        //登录失败,转发到登录页面
        request.setAttribute("mess", "账号和密码不能为空!");
        request.getRequestDispatcher("index.jsp").forward(request,
                response);
    }
    //操作service层从数据库查询用户
    User user = us.longin(userName, pwd);

    //3.跳转
    if(user==null){
        //登录失败,转发到登录页面
        request.setAttribute("mess", "账号或密码错误,请检查!");
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }else{
        //将用户登录信息保存到session域
        session.setAttribute("user", user);

        //判断用户是否要求记住密码
        if(isPwd!=null && isPwd.equals("on")){
            //1.保存用户的信息到Cookie对象中
            Cookie c1 = new Cookie("userName",user.getUsername());
            Cookie c2 = new Cookie("pwd",user.getPassword());
            Cookie c3 = new Cookie("isPwd","1");
            //2.设置cookie有效期
            c1.setMaxAge(60*60*24*7);
            c2.setMaxAge(60*60*24*7);
            c3.setMaxAge(60*60*24*7);
            //3.把cookie发送给客户端浏览器
            response.addCookie(c1);
            response.addCookie(c2);
            response.addCookie(c3);
        }else{
            //清除cookie
            Cookie[] cs = request.getCookies();
            if(cs!=null){
                for(Cookie c : cs){
                    if(c.getName().equals("userName")){
                        c.setMaxAge(0);
                        //把修改后的cookie对象重新发送给客户端
                        response.addCookie(c);
                    }

                    if(c.getName().equals("pwd")){
                        c.setMaxAge(0);
                        //把修改后的cookie对象重新发送给客户端
                        response.addCookie(c);
                    }
                    if(c.getName().equals("isPwd")){
                        c.setMaxAge(0);
                        //把修改后的cookie对象重新发送给客户端
                        response.addCookie(c);
                    }
                }
            }
        }

        //重定向到系统首页
        response.sendRedirect("shouye.jsp");
    }
%>

首页

<%@ page language="java"
    import="java.util.*,com.lyb.ShiTiLei.*,com.lyb.service.*"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
    Object obj = session.getAttribute("user");
        if(obj==null){
            //用户没有登陆的情况,重定向到登录页面
            response.sendRedirect("index.jsp");
        }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>学生信息管理系统--首页</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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<style type="text/css">
#addstu {
    text-decoration: none;
    border: 1px solid blue;
    width: 80px;
    height: 30px;
    text-align: center;
    line-height: 30px;
}

#addstu:HOVER {
    background-color: blue;
    color: #fff;
}

#from {
    float: right;
    margin-right: 100px;
}
</style>
<script>
    //声明一个验证删除的方法
    function del(name, id) {
        //弹出警告框
        //alert("hello");
        //弹出选择框 
        var rel = confirm("你确定要删除" + name + "吗?");
        if (rel) {
            location.assign("m_shanchu.jsp?id=" + id);
        }
    }
</script>

</head>
<body>
    <%
        //设置请求参数的编码
        request.setCharacterEncoding("utf-8");
       //   response.setCharacterEncoding("UTF-8");
        //接收查询的姓名参数
        String stuName = request.getParameter("stuName");
        out.print(stuName);
        if (stuName == null) {
            stuName = "";
        }

        //接收当前页数的参数,封装当前页数
        String s = request.getParameter("dqy");
        int dqy = 0;
        if (s == null || s.trim().isEmpty()) {
            dqy = 1;
        } else {
            dqy = Integer.valueOf(s);
        }

        //查询所有学生
        StuService ss = new StuService();
        FenYe<Stu> pb = ss.findAll(dqy,stuName);
    %>

    <h1 align="center">学生管理系统 V2.0</h1>
    <p align="center">
        当前用户:${sessionScope.user.username}&nbsp;&nbsp;<a href="tuichu.jsp">退出登录</a>
    </p>
    <hr>
    <p>
    <form id="form" action="shouye.jsp" method="post">
        请输入要查询的学生姓名:<input type="text" name="stuName">
         <input type="submit" value="查询">
    </form>
    </p>
    <p style="padding-left:200px;">
        <a id="addStu" href="addstu.jsp">添加学生</a> &nbsp;&nbsp;
    </p>
    <table border="1" width="%80" align="center">
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>年级</th>
            <th>选择操作</th>
        </tr>
        <%
            if (pb.getPageList() != null && pb.getPageList().size() > 0) {
                for (Stu stu : pb.getPageList()) {
        %>

        <tr align="center">
            <td><%=stu.getStuNo()%></td>
            <td><%=stu.getStuName()%></td>
            <td><%=stu.getSex()%></td>
            <td><%=stu.getAge()%></td>
            <td><%=stu.getGrade()%></td>
            <td><a href="xiugai.jsp?id=<%=stu.getId()%>">修改</a> &nbsp; <a
                href="javascript: del('<%=stu.getStuName()%>',<%=stu.getId()%>) ;">删除</a>
            </td>
        </tr>

        <%
            }
            }
        %>
    </table>

    <p style="text-align: center;margin-top: 30px">

        当前第<%=pb.getDqy()%>页/共<%=pb.getZys()%>页 &nbsp;

        <%
            if (pb.getDqy() == 1) {
                out.print("上一页\t");
                out.print("<a href='shouye.jsp?dqy="+(pb.getDqy() + 1)
                        + "&stuName=" + stuName + "'>下一页</a>");
            } else if (pb.getDqy() == pb.getZys()) {
                out.print("<a href='shouye.jsp?dqy="+(pb.getDqy() - 1)
                        + "&stuName=" + stuName + "'>上一页</a>\t");
                out.print("下一页");
            } else {
                out.print("<a href='shouye.jsp?dqy="+(pb.getDqy() - 1)
                        + "&stuName=" + stuName + "'>上一页</a>\t");
                out.print("<a href='shouye.jsp?dqy=" + (pb.getDqy() + 1)
                        + "&stuName=" + stuName + "'>下一页</a>");
            }
        %>
    </p>
    <font color="red">${mess }</font>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值