java过滤器

 

ps:本人破解了润乾报表,全部功能可用,低价销售,需要要的请加qq:229501642联系

 

实现简单的页面过滤,比如只能通过登陆才能登陆系统:代码如下

//配置了两个过滤器,SetCharacterEncodingFilter 是字符集过滤器,SessionFilter 是页面过滤器

package com.project.glq;

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 SetCharacterEncodingFilter implements Filter{
 FilterConfig config=null;
 public void init(FilterConfig config) throws ServletException{
  
  this.config=config;
 }
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{
  String encoding=config.getInitParameter("gbk");
  //System.out.println("过滤器1请求处理开始");
  request.setCharacterEncoding(encoding);
  response.setCharacterEncoding(encoding);
  chain.doFilter(request,response);
  //System.out.println("过滤器1响应处理开始");
 }
 public void destroy(){
  
 }
 

}

 

package com.project.glq;

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.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;

import com.project.po.EmpPo;

public class SessionFilter implements Filter{
 FilterConfig config=null;
  public static boolean isContains(String container, String[] regx) {
         boolean result = false;

         for (int i = 0; i < regx.length; i++) {
             if (container.indexOf(regx[i]) != -1) {
                 return true;
             }
         }
         return result;
     }
 public void init(FilterConfig config) throws ServletException{
  
  this.config=config;
 }
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{
  //System.out.println("过滤器2请求处理开始");
  HttpServletRequest httpreq = (HttpServletRequest) request;
  HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
  HttpSession session=httpreq.getSession();
  //System.out.println("请求路径:"+httpreq.getRequestURI());
  Object emppo=session.getAttribute("emppo");//获取用户对象
  String logonStrings = config.getInitParameter("logonStrings");//获取web.xml文件配置参数login.jsp
  String includeStrings = config.getInitParameter("includeStrings");//获取web.xml文件配置参数.jsp;.html
        String[] logonList = logonStrings.split(";");
        String[] includeStringlist = includeStrings.split(";");
        String redirectPath = httpreq.getContextPath()+"/login.jsp";
  
  if(emppo==null){//判断用户是否登陆
    if (this.isContains(httpreq.getRequestURI(), logonList)) {
     //System.out.println("这是login.jsp等请求执行的代码");
                 chain.doFilter(request, response);//执行下一个过滤器
                // System.out.println("过滤器2响应处理开始");
                return;
             }
    if (!this.isContains(httpreq.getRequestURI(), includeStringlist)) {
    // System.out.println("这是非.jsp;.html等请求执行的代码");
                 chain.doFilter(request, response);
                 //System.out.println("过滤器2响应处理开始");
                 return;
             }
   //System.out.println("这是.jsp;.html等请求执行的代码,非login.jsp");
            wrapper.getResponse().getWriter().write("<script language =javascript>window.location.href='"+redirectPath+"';</script>");
            wrapper.getResponse().getWriter().close();
            chain.doFilter(request, response);
           // System.out.println("过滤器2响应处理开始");
            return;
     }
  else{
   chain.doFilter(request, response);
  // System.out.println("过滤器2响应处理开始");
  }
  
  
 }
 public void destroy(){
  
 }
}

 

web.xml文件配置如下:

<filter>
  <filter-name>filter</filter-name>
  <filter-class>com.project.glq.SetCharacterEncodingFilter</filter-class>
  <init-param>
     <param-name>gbk</param-name>
     <param-value>gbk</param-value>
     </init-param>
 </filter>
 
 <filter>
  <filter-name>sessionfilter</filter-name>
  <filter-class>com.project.glq.SessionFilter</filter-class>
  <init-param>
       <param-name>logonStrings</param-name>
       <param-value>login.jsp</param-value>
     </init-param>
     <init-param>
        <param-name>includeStrings</param-name>
        <param-value>.jsp;.html</param-value>
    </init-param>
 </filter>
  
 <filter-mapping>
  <filter-name>filter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
 </filter-mapping>
 <filter-mapping>
  <filter-name>sessionfilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

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