17、登录案例

主页面处理

使用Session技术,主页面只有用户名和密码

/** * 用户主页的逻辑 * */
public class IndexServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();


        String html = "";

        /** * 接收request域对象的数据 */
        /* String loginName = (String)request.getAttribute("loginName"); */

        /** * 二、在用户主页,判断session不为空且存在指定的属性才视为登录成功!才能访问资源。 * 从session域中获取会话数据 */
        //1.得到session对象
        HttpSession session = request.getSession(false);
        if(session==null){
            //没有登录成功,跳转到登录页面
            response.sendRedirect(request.getContextPath()+"/login.html");
            return;
        }
        //2.取出会话数据
        String loginName = (String)session.getAttribute("loginName");
        if(loginName==null){
            //没有登录成功,跳转到登录页面
            response.sendRedirect(request.getContextPath()+"/login.html");
            return;
        }

        html = "<html><body>欢迎回来,"+loginName+",<a href='"+request.getContextPath()+"/LogoutServlet'>安全退出</a></body></html>";


        writer.write(html);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

登录处理

/** * 处理登录的逻辑 * */
public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接收参数
        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");

        //2.判断逻辑
        if("eric".equals(userName)
                 && "123456".equals(userPwd)){
            //登录成功
            /** * 分析: * context域对象:不合适,可能会覆盖数据。 * request域对象: 不合适,整个网站必须得使用转发技术来跳转页面 * session域对象:合适。 */
            /* request.setAttribute("loginName", userName); //request.getRequestDispatcher("/IndexServlet").forward(request, response); response.sendRedirect(request.getContextPath()+"/IndexServlet"); */

            /** * 一、登录成功后,把用户数据保存session对象中 */
            //1.创建session对象
            HttpSession session = request.getSession();
            //2.把数据保存到session域中
            session.setAttribute("loginName", userName);
            //3.跳转到用户主页
            response.sendRedirect(request.getContextPath()+"/IndexServlet");

        }else{
            //登录失败
            //请求重定向
            response.sendRedirect(request.getContextPath()+"/fail.html");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

登出处理

/** * 退出逻辑 * */
public class LogoutServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /** * 三、安全退出: * 删除掉session对象中指定的loginName属性即可! */
        //1.得到session对象
        HttpSession session = request.getSession(false);
        if(session!=null){
            //2.删除属性
            session.removeAttribute("loginName");
        }

        //2.回来登录页面
        response.sendRedirect(request.getContextPath()+"/login.html");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

你可能感兴趣的:(17、登录案例)