zoukankan      html  css  js  c++  java
  • JavaWeb——使用会话维持状态3

    这次的例子是使用会话给上一个例子添加登陆功能

    1、页面逻辑

    • 首先是登陆页面,这里需要输入账号和密码,输入正确后将进入商品列表页面,输入错误将会提示账号或者密码错误

    • 其次是商品列表和购物车页面,添加了注销的链接

     

    2、JSP

    • 登陆页面的JSP,,loginFailed若为true表示用户已经尝试登陆且登陆出错,若为false表示用户还没用尝试登陆
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>login application</title>
    </head>
    <body>
        <h2>Login</h2>
        You must log in to access the customer support site.<br /><br />
        <%
            if(((Boolean)request.getAttribute("loginFailed")))
            {
        %>
        <b>The username or password you entered are not correct. Please try
            again.</b><br /><br />
        <%
            }
        %>
        <form method="POST" action="<c:url value="/login" />">
            Username<br />
            <input type="text" name="username" /><br /><br />
            Password<br />
            <input type="password" name="password" /><br /><br />
            <input type="submit" value="Log In" />
        </form>
    </body>
    </html>
    • 然后就是把这行代码加到商品列表和购物车页面
     <a href="<c:url value="/login?logout=false" />">Logout</a>

    3、代码逻辑

    • 先是简单的建立账号密码数据库
        private static final Map<String, String> userDatabase = new Hashtable<>();
        static {
            userDatabase.put("user1","password1");
            userDatabase.put("user2","password2");
            userDatabase.put("user3","password3");
            userDatabase.put("user4","password4");
        }
    • 然后是doGet方法的逻辑,注销的链接是直接重定向到登陆页面的,所以登陆页面先是判断是否需要清空Session,然后通过username检测是否已经登陆过了,然后就是将loginFailed设为false并将请求转发给login.jsp
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            if(request.getParameter("logout") != null)
            {
                request.getSession().invalidate();
                response.sendRedirect("login");
                return;
            }else if(session.getAttribute("username" )!= null)
            {
                response.sendRedirect("shop");
                return;
            }
    
            request.setAttribute("loginFailed",false);
            request.getRequestDispatcher("/WEB-INF/jsp/view/login.jsp")
                    .forward(request, response);
        }
    • doPost将在用户点击提交时被调用,同样首先通过username检测是否已经登陆过,如果没有登陆过将检查账号密码是否有问题,没有问题重定向到商品列表页面,如果有问题的话将会把loginFailed设为true并将请求转发给login.jsp
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            if(session.getAttribute("username" )!= null)
            {
                response.sendRedirect("shop");
                return;
            }
    
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if(username == null || password == null ||
                    !LoginServlet.userDatabase.containsKey(username)||
                    !password.equals(LoginServlet.userDatabase.get(username))){
                request.setAttribute("loginFailed",true);
                request.getRequestDispatcher("/WEB-INF/jsp/view/login.jsp")
                        .forward(request,response);
            } else {
              session.setAttribute("username", username);
              request.changeSessionId();
              response.sendRedirect("shop");
            }
        }
  • 相关阅读:
    F系列车牌识别设备
    金蝶云星空安装及卸载教程
    RG-RAC256高性能无线控制器
    关于IP网段划分
    Win10关闭自动更新的三种方法
    锐捷网络RG-S2528G-24P
    光纤信号的传输距离
    POE交换机
    光纤收发器
    大华工具管家 1.01.1官方版
  • 原文地址:https://www.cnblogs.com/xxbbtt/p/8404239.html
Copyright © 2011-2022 走看看