zoukankan      html  css  js  c++  java
  • filter

    web.xml

    <filter>
    <filter-name>encoding</filter-name>
    <filter-class>com.filter.EncodingFilter</filter-class>
    <init-param>
    <param-name>charset</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    class:

    package com.filter;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;

    public class LoginFilter implements Filter {

    public void destroy() {
    // TODO Auto-generated method stub

    }

    //执行过滤
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    //向下转型(因为session本身是属于http协议范畴)
    HttpServletRequest req = (HttpServletRequest) request;
    //取得session
    HttpSession ses = req.getSession();

    //判断是否登录
    if (ses.getAttribute("userid") != null) {
    chain.doFilter(request, response);//传递请求,继续判断其他验证
    } else {
    //跳转到登陆页
    request.getRequestDispatcher("login.jsp").forward(request, response);
    }
    }

    public void init(FilterConfig arg0) throws ServletException {
    // TODO Auto-generated method stub

    }

    }

  • 相关阅读:
    盗COOKIE之方法总结
    会话追踪(session tracking)
    XSS盗COOKIE
    ActiveX 控件漏洞挖掘之方法
    Windows之权限讲解
    Centos网络配置小工具
    区分一下dpkg,rpm和yum以及apt-get
    Java中弹出框的集中方式
    Spring3 MVC请求参数获取的几种方法
    Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3022312.html
Copyright © 2011-2022 走看看