filter的应用

1、设置字符集

package net.company.web.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;

public class CharacterEncoding implements Filter {
    private String encode;
 public void destroy() {
 }
 public void doFilter(ServletRequest req, ServletResponse resp,
   FilterChain chain) throws IOException, ServletException {
  //设置所以的request和response的字符集为encode
  req.setCharacterEncoding(encode);
  resp.setCharacterEncoding(encode);
  chain.doFilter(req, resp);
 }

 public void init(FilterConfig config) throws ServletException {
  //从web.xml中读取配置信息
  this.encode=config.getInitParameter("code");
 }

}

 

 

2、粗粒度的权限控制

package net.company.web.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;

import net.company.entity.Employees;

public class PrivilegeFilter implements Filter{

 public void destroy() {
  // TODO Auto-generated method stub
  
 }
    //粗粒度的权限控制
 public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain chain) throws IOException, ServletException {
  HttpServletRequest req=(HttpServletRequest)arg0;
  HttpServletResponse resp=(HttpServletResponse)arg1;
  Employees employee=(Employees)req.getSession().getAttribute("employee");
  if(employee==null){
   req.setAttribute("result", "please logon ....");
   req.getRequestDispatcher("/logon.jsp").forward(req, resp);
      return;
  }
  chain.doFilter(req, resp);
  
 }

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

}

 

阅读更多
文章标签: filter class string null
个人分类: ssh
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

filter的应用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭