web.xml 配置
<!-- 强制用户登陆过滤 --> <filter> <filter-name>login</filter-name> <filter-class>com.yongjie.Zlxypt.util.FileLogin</filter-class> </filter> <filter-mapping> <filter-name>login</filter-name> <!-- 对那些文件过滤 --> <url-pattern>/jsp/*</url-pattern> </filter-mapping>
2.代码实现:
package com.yongjie.Zlxypt.util;
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.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class FileLogin implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
//转换成http
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
//获取session 对象
HttpSession session = request.getSession(true);
Object obj = session.getAttribute(SystemConstant.SYSTEM_SESSION_NAME);
if(obj == null){
response.sendRedirect(request.getContextPath()+SystemConstant.SYSTEM_LOGIN_JSP);
}else{
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
}
}
转载于:https://blog.51cto.com/zhuws/1969504