过滤器

com.hotel.filter.EncodeFilter:

/**
 * 
 */
package com.hotel.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;

/**
 * @author Administrator
 *
 */
public class EncodeFilter implements Filter {
	
	// 定义编码格式的属性
	private String encode;

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		System.out.println("过滤器销毁");

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("执行过滤器");
		req.setCharacterEncoding(encode);
		resp.setContentType("text/html;charset="+encode);
		// 让过滤器转发请求
		chain.doFilter(req, resp);

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig config) throws ServletException {
		System.out.println("过滤器初始化");
		// 读取过滤器中初始化的编码格式
		String encode = config.getInitParameter("encode");
		this.encode =  encode;
	}

}

com.hotel.filter.PermissionFilter:

package com.hotel.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.HttpServletResponse;

/**
 * Servlet Filter implementation class PermissionFilter
 */
public class PermissionFilter implements Filter {

    /**
     * Default constructor. 
     */
    public PermissionFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		// 定义非检查列表
		String[] noCheckList= {
				"loginServlet",
				"addRoomInfoServlet",
				"login.jsp"
		};
		
		// 把servlet请求和响应对象强制转换为HTTP的请求响应对象
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		
		// 获取当前用户请求地址
		String uri = request.getRequestURI();
		// 通过得到的uri路径获取当前访问资源的名称
		String resource = uri.substring(uri.lastIndexOf("/")+1);
		
		// 判断访问资源是否在非检查列表中
		for(String urlName : noCheckList) {
			if(resource.equals(urlName)) {
				chain.doFilter(request, response);
				return;
			}
		}
		
		// 判断用户是否登录[判断session对象中是否存在值]
		Object object = request.getSession().getAttribute("userName");
		if(object == null) {
			response.sendRedirect("login.jsp");
		} else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

配置文件:

 <filter>
    <filter-name>encode</filter-name>
    <filter-class>com.hotel.filter.EncodeFilter</filter-class>
    <init-param>
      <param-name>encode</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encode</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <display-name>PermissionFilter</display-name>
    <filter-name>PermissionFilter</filter-name>
    <filter-class>com.hotel.filter.PermissionFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>PermissionFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


你可能感兴趣的:(encode,过滤器,package,public)