JSP用户登陆注销页面

1、userLogin页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/10/15
  Time: 12:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
    // 登录页面读取Cookie
<%
    Cookie [] cookies = request.getCookies();
    String username = "";
    for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("name")) {
            username =  cookies[i].getValue();
        }
    }
%>
<form action="loginSuccess.jsp" method="post">
    <table >
        <tr>
            <td>用户名&nbsp;<input type="text" name="username" value="<%=username%>" required></td>
        </tr>
        <tr>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password" required></td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="登录">
                <input type="reset" value="重置">
            </td>
        </tr>
    </table>
</form>
<%
    // 设置请求的编码方式
    request.setCharacterEncoding("UTF-8");
%>
</body>
</html>

2、loginSuccess页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/10/15
  Time: 12:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录后页面</title>
</head>
<body>
<%
    String name=request.getParameter("username");// 取得 name 的信息
    String pwd=request.getParameter("password");  // 取得 pwd 的信息
%>
<p>姓名:<%=name%></p>
<p>密码:<%=pwd%></p>
<%
    boolean isSuccess=false;
        // 用户名:admin 密码:123
        // 进行用户名和密码的验证
    if(name.equals("admin") && pwd.equals("123")){
            isSuccess=true;
        // setAttribute方法 对request对象的属性进行保存
        request.setAttribute("msg","恭喜管理员大人登录成功!");
        // 登录成功用户名和密码保存在session中
        session.setAttribute("uName",name);
        session.setAttribute("uPWD",pwd);
        // 添加到cookie
        Cookie cookie = new Cookie("name", name);
        // 设置有效期1天
        cookie.setMaxAge(60*60*24);
        // 添加到cookie
        response.addCookie(cookie);
        // 跳转至logout.jsp页面中
        //response.sendRedirect("logout.jsp");
        response.setHeader("refresh", "2; URL = logout.jsp");  // 定时2秒跳转
    }else {
        request.setAttribute("msg","登入失败!");
        // 转发到userLogin.jsp页面中
        request.getRequestDispatcher("userLogin.jsp").forward(request,response);
    }
        // getAttribute方法 对request对象的属性进行读取
         String result=(String)request.getAttribute("msg");
%>
    <h3><%=result%></h3>
<%--<input type="text" value=<%=session.getAttribute("uName")%> readonly>--%>
</body>
</html>

3、logout页面

<%--
  Created by IntelliJ IDEA.
  User: mu_bai
  Date: 2019/10/15
  Time: 19:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注销页面</title>
</head>
<body>
<%
    String name = (String) session.getAttribute("uName");
    String pwd = (String) session.getAttribute("uPWD");
    String result = (String) session.getAttribute("msg");

    if (name != null || pwd != null || result != null) {

%>
<p>用户名:<%=name%></p>
<p>密码:<%=pwd%></p>
<%}
    //注销 session
    session.invalidate();
%>
<form action="userLogin.jsp" method="post">
    <input type="submit" name="注销" value="注销">
</form>
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值