黑马旅游网-学习笔记Part06

YangeIT大约 5 分钟旅游项目Mysql

黑马旅游网-学习笔记Part06

今日目标

  • 登录过滤器 🍐✏️
  • 主页导航栏实现 ❤️
  • Redis优化导航栏查询所有

知识储备

  1. 了解session和cookie的缺陷
  2. 已经完成过滤器拦截登录代码

1. 登录过滤器

登录过滤器

访问页面详情时候,需要校验是否登录,如果未登录,直接跳转到登录页面 需求

点击查看过滤器流程open in new window 👈 👈

代码操作

  1. 在filter包创建LoginCheckFilter类 实现Filter接口,配置拦截路径为所有

@Slf4j 注解是日志注解,导入下列依赖

log.info中{} 是占位符的意思,逗号后面的值即为变量

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>
/**
 * 检查用户是否已经完成登录
 * filterName:过滤器在对象容器中的名字
 * urlPatterns:匹配路径
 */
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    //todo 书写拦截逻辑
    System.out.println("我被拦截了");
    }

    @Override
    public void destroy() {

    }



}

2. 主页导航栏实现

主页导航栏实现

需求效果: 👇

需求描述
需求描述

接口信息: 👇

  • 请求路径:/travel/categoryServlet
  • 请求方式:GET
  • 请求参数:无参数
  • 返回数据:category集合的json字符串

代码操作image

  1. 检查pom.xml是否有Alibaba的fastjson依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>
  1. 在控制层CategoryServlet中书写接受参数、查询数据、返回数据等逻辑s

@WebServlet("/categoryServlet")
public class CategoryServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //从数据库中拿到 所有的数据 回显给前端
        CategoryService categoryService = new CategoryServiceImpl();
        List<Category> list  =   categoryService.findAll();

        String json = JSON.toJSONString(list);
        //设置响应头信息
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

3.Redis优化导航栏查询所有

前言

背景: 门户网站每次访问时候查询的分类数据是一样的,且每次都要查询数据库,如果用户激增,会造成数据库访问压力,因此需要使用redis进行优化

核心逻辑: 先查询缓存,如果存在,直接返回数据,如果不存在,然后查询数据库,存入缓存,并返回

点击查看核心逻辑图解open in new window 👈 👈

代码操作

1.检查redisclient依赖是否导入

<!--jedis-->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.0</version>
    </dependency>

2.检查JedisUtil工具类是否导入成功!

public final class JedisUtil {
    private static JedisPool jedisPool;

    static {
        //读取配置文件
        InputStream is = JedisPool.class.getClassLoader().getResourceAsStream("jedis.properties");
        //创建Properties对象
        Properties pro = new Properties();
        //关联文件
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取数据,设置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

        //初始化JedisPool
        jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));


    }


    /**
     * 获取连接方法
     */
    public static Jedis getJedis() {
        return jedisPool.getResource();
    }

    /**
     * 关闭Jedis
     */
    public static void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

3.检查redis服务端是否启动,并且配置文件是否配置正确image

host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10