MVC系列教材 (四)- 使用Servlet 验证用户是否登陆

比如网站提供Hero查询服务,但是前提是用户要登录过才能使用。 

如果用户登陆过了,访问listHero,就让用户正常访问,否则就跳转到登陆界面。 

这是非常常见的场景,通过使用 session 来实现这个功能。 

在处理登录的loginServlet 中使用将用户名保存在session中。 

HeroListServlet 中查看session中是否为空。如果为空,就表示用户没有登陆过,就跳转到登陆页面

步骤1:在LoginServlet 把验证成功的用户加入到 Session
步骤2:在HeroListServlet判断Session中是否有数据

步骤 1 : 在LoginServlet 把验证成功的用户加入到 Session

如果用户输入正确的账号密码,就跳转到 listHero,并且把用户名以"userName"放进session

如果用户输入错误的账号密码,就跳转到 login.html,让用户重新登陆

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String name = request.getParameter("name");

        String password = request.getParameter("password");

        if ("admin".equals(name) && "123".equals(password)) {

            request.getSession().setAttribute("userName", name);

            response.sendRedirect("listHero");

        else {

            response.sendRedirect("login.html");

        }

    }

}

步骤 2 : 在HeroListServlet判断Session中是否有数据

String userName = (String) request.getSession().getAttribute("userName");

   if (null == userName) {

   response.sendRedirect("login.html");

   return;

}



从session中取出userName,如果是空,就表示用户没有登录,或者登录已经超过了30分钟。 客户端跳转到login.html,让用户重新登陆

package servlet;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import bean.Hero;

import dao.HeroDAO;

public class HeroListServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String userName = (String) request.getSession().getAttribute("userName");

        if (null == userName) {

            response.sendRedirect("login.html");

            return;

        }

        int start = 0;

        int count = 5;

        try {

            start = Integer.parseInt(request.getParameter("start"));

        catch (NumberFormatException e) {

            // 当浏览器没有传参数start时

        }

        int next = start + count;

        int pre = start - count;

        int total = new HeroDAO().getTotal();

        int last;

        if (0 == total % count)

            last = total - count;

        else

            last = total - total % count;

        pre = pre < 0 0 : pre;

        next = next > last ? last : next;

        request.setAttribute("next", next);

        request.setAttribute("pre", pre);

        request.setAttribute("last", last);

        List<Hero> heros = new HeroDAO().list(start, count);

        request.setAttribute("heros", heros);

        request.getRequestDispatcher("listHero.jsp").forward(request, response);

    }

}


更多内容,点击了解: https://how2j.cn/k/mvc/mvc-authentication/591.html

原文地址:https://www.cnblogs.com/Lanht/p/12789442.html