用户登陆过滤器

CheckLoginFilter用户登陆过滤器

package com.student.comm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

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 CheckLoginFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;     
		HttpServletResponse response = (HttpServletResponse) servletResponse;     
		   
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		if(username == null || "".equals(username)){
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		}else{
			filterChain.doFilter(servletRequest, servletResponse); 
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException     
	{     
		   
	}     
}


web.xml配置
<!-- =========================================== -->
	<!-- ============ 过滤器映射 ============== -->
	<!-- =========================================== -->
	<!-- 登陆过滤器映射 -->
  <filter>
		<filter-name>CheckLoginFilter</filter-name>
		<filter-class>
			com.student.comm.CheckLoginFilter
		</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>CheckLoginFilter</filter-name>
		<url-pattern>/admin/*</url-pattern>
	</filter-mapping>

你可能感兴趣的:(java,xml,jsp,Web,servlet)