zoukankan      html  css  js  c++  java
  • com.jsj.filter====新闻发布系统(7)

    2020-05-15

    这个是拦截器,可以看做保安

    com.jsj.filter

    AdminFilter

     1 package com.jsj.filter;
     2 
     3 import javax.servlet.*;
     4 import javax.servlet.annotation.WebFilter;
     5 import javax.servlet.http.HttpServletRequest;
     6 import javax.servlet.http.HttpServletResponse;
     7 import java.io.IOException;
     8 
     9 /**
    10  * 管理员认证拦截
    11  */
    12 @WebFilter(urlPatterns = "/admin/manage/*")
    13 public class AdminFilter implements Filter {
    14 
    15     @Override
    16     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    17         HttpServletRequest request = (HttpServletRequest) servletRequest;
    18         HttpServletResponse response = (HttpServletResponse) servletResponse;
    19         if (request.getSession().getAttribute("adminStatus")!=null&& (boolean) request.getSession().getAttribute("adminStatus"))
    20             filterChain.doFilter(servletRequest, servletResponse);
    21         else {
    22             response.setContentType("text/html;charset=UTF-8");
    23             response.getWriter().println("您不是管理员,无权访问该资源!请进行<a href='/admin'>管理员登录</a>");
    24         }
    25     }
    26 
    27 }
    View Code

    CharterEncodingFilter

     1 package com.jsj.filter;
     2 
     3 import javax.servlet.*;
     4 import javax.servlet.annotation.WebFilter;
     5 import javax.servlet.annotation.WebInitParam;
     6 import javax.servlet.http.HttpServletRequest;
     7 import java.io.IOException;
     8 
     9 /**
    10  * 请求编码过滤器
    11  */
    12 @WebFilter(urlPatterns = "/*",initParams = { @WebInitParam(name = "CharacterEncoding", value = "UTF-8")})
    13 public class CharacterEncodingFilter implements Filter {
    14 
    15     private String characterEncoding;
    16 
    17     @Override
    18     public void init(FilterConfig filterConfig) {
    19         this.characterEncoding = filterConfig.getInitParameter("CharacterEncoding");
    20     }
    21 
    22     @Override
    23     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    24         HttpServletRequest request = (HttpServletRequest) servletRequest;
    25         request.setCharacterEncoding(characterEncoding);
    26         filterChain.doFilter(request,servletResponse);
    27     }
    28 }
    View Code
    会当凌绝顶,一览众山小
  • 相关阅读:
    [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的元素
    [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
    [LeetCode] 146. LRU Cache 近期最少使用缓存
    [LeetCode] 381. Insert Delete GetRandom O(1)
    [LeetCode] 380. Insert Delete GetRandom O(1) 插入删除获得随机数O(1)时间
    [LeetCode] 57. Insert Interval 插入区间
    [LeetCode] 56. Merge Intervals 合并区间
    [LeetCode] 155. Min Stack 最小栈
    移动web开发-------meta
    font-family
  • 原文地址:https://www.cnblogs.com/quenvpengyou/p/12896292.html
Copyright © 2011-2022 走看看