zoukankan      html  css  js  c++  java
  • Filter

    1.字符集

    @WebFilter(filterName="EcodingFilter",urlPatterns="/*")
    
    public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            chain.doFilter(request,response);
        }

    2.限制没有登录的用户

    设置一个path文件夹,里面的页面只要登录之后才可以查看

    建立一个LoginFilter用来查看session中有没有用户登录的信息,如果没有就拦截,如果有就允许登录

    @WebFilter(filterName="LoginFilter",urlPatterns="/path/*")

    public
    void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest r = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; HttpSession session = r.getSession(); String user = (String)session.getAttribute("username"); if(user != null){ chain.doFilter(request, response); }else { String msg = "没有登录"; r.setAttribute("error", msg); r.getRequestDispatcher("/PleaseLogin.jsp").forward(r, res); } }

     PleaseLogin.jsp

    用来显示错误的error

    <body>
        ${error }
    </body>
  • 相关阅读:
    简单的语句统计所有用户表尺寸大小
    CodeSmith 介绍
    Oracle Partition By 的使用
    Oracle Contact By的使用
    正则提取 html 里<input> 标记的value 值
    IOS 7 风格Checkbox
    aspose words 介绍
    大规模web 服务开发技术
    数学之美 读后感
    工作流简介--(转)
  • 原文地址:https://www.cnblogs.com/da-peng/p/5780612.html
Copyright © 2011-2022 走看看