zoukankan      html  css  js  c++  java
  • JSP简单实现登录和注销

    JSP简单实现登录和注销

    需求:用户登录成功后跳转到欢迎页面

              用户登录失败跳转到初始的登录界面

              用户点击注销,用户退出登录状态需要重新登录

    登录页面的JSP代码:

    <%@ 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>登录页面</title>
    </head>
    <body>
    <form action="do_login.jsp" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <br>
    <input type="submit" value="确认">
    <input type="reset" value="重置">
    </form>
    </body>
    </html>

    处理登录的JSP代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    if(username.equals("xiongda")&&password.equals("123")){
        session.setAttribute("username", username);
        response.setHeader("Refresh", "2,URL=welcome.jsp");
    }else{
        response.setHeader("Refresh", "2,URL=login.jsp");
    }
    %>

    欢迎界面的JSP代码:

    <%@ 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>欢迎页面</title>
    </head>
    <body>
    <%if(session.getAttribute("username")!=null){ %>
    你好,<%=session.getAttribute("username") %>
    <a href="logout.jsp">注销</a>
    <br>
    <%}else{%>
    请先登录:<a href="login.jsp">登录</a>
    <br>
    <%} %>
    <% if(session.isNew()){ %>
    欢迎新用户!
    <%}else{ %>
    欢迎老用户!
    <%} %>
    </body>
    </html>

    注销的JSP代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%session.invalidate();
        response.setHeader("Refresh", "2,URL=welcome.jsp");
        %>

    核心思想:

    登陆成功后就将用户的信息存入session中,如果用户需要注销的时候就清除session对象

  • 相关阅读:
    crm-ssh-列表显示(顾客列表,用户,联系人列表)
    leetcode- Rotate Array 旋转数组
    ssh的整合
    svn详解和使用
    leetcode-Plus One 加一
    spring-jdbc-aop事务
    leetcode-Remove Duplicates from Sorted Array
    0020 DRF框架开发(07 基类视图 GenericAPIView)
    0019 DRF框架开发(06 基类视图 APIView)
    0018 DRF框架开发(05 序列化器的字段与选项)
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/8996314.html
Copyright © 2011-2022 走看看