权限管理过滤器,filter,session,页面或者哪个文件下的所有页面进行登陆过滤

2007-12-27 22:39JAVA源文件:

package org.filter.system;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
import javax.servlet.*;
import org.bean.SessionBean;   
/** 
* Title:        LOGIN FILTER. 
*
* Description: System user login servlet filter. 
*
* Copyright:    Copyright (c) 2007/10/05
*
* Company:      JaMing SoftRoom
*
* @author: Jamee
*
* @version 1.0 
*/
public class SysLoginFilter implements Filter{

    FilterConfig config;

    public void init(FilterConfig config)throws ServletException

    {

    this.config=config;

    }
    public void doFilter(ServletRequest request,ServletResponse response,

                       FilterChain chain)throws IOException,ServletException
    {

        HttpServletRequest hreq=(HttpServletRequest)request;

        HttpServletResponse hrep=(HttpServletResponse)response;

        request.setCharacterEncoding("GBK");

        HttpSession session=hreq.getSession();

        String back="default.jsp";

        RequestDispatcher dispatcher=hreq.getRequestDispatcher(back);

        try{

            SessionBean user=(SessionBean)session.getAttribute("user");

            boolean login=user.getIsLogin();

            if(login)
            {

                chain.doFilter(request,response);   

            }else{

                dispatcher.forward(request,response);

            }

        }catch(Exception e){

            dispatcher.forward(request,response);

        }
        
    }

    public void destroy(){}

}

 ----附SessionBean.java源文件------------

package org.bean;
/** 
* Title:        Session. 
*
* Description: Session. 
*
* Copyright:    Copyright (c) 2007/9/27
*
* Company:      JaMing SoftRoom
*
* @author: Jamee
*
* @version 1.0 
*/
public class SessionBean
{

    int QX=4;

    String LOGIN_NAME;

    boolean IS_LOGIN=false;

    public void SessionBean(){}

    public void setLoginName(String name)

    {
      
       this.LOGIN_NAME=name;
    }

    public void setIsLogin(boolean l)
    {

         this.IS_LOGIN=l;
    }

    public void setQX(int i)
     
    {

        this.QX=i;

    }

    public String getLoginName()

    {

        return this.LOGIN_NAME;

    }

    public boolean getIsLogin()

    {

        return this.IS_LOGIN;

    }
    
    public int getQX()

    {

        return this.QX;

    }
    public String getAdmin()

    {

        String admin=null;


        int i=getQX();


        if(i==0)admin="系统管理员";


        if(i==1)admin="行政管理员";


        if(i==2)admin="教务管理员";


        if(i==3)admin="德育管理员";


        if(i==4)admin="游客";


        return admin;

    }
}

 -------附web.xml配置---------

   

 <filter>
        <filter-name>ADMIN SESSION FILTER</filter-name>
        <filter-class>org.filter.system.SysLoginFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>ADMIN SESSION FILTER</filter-name>
<url-pattern>/admin/*</url-pattern>
    </filter-mapping>

 解释:

如果你要对哪个页面或者哪个文件下的所有页面进行登陆过滤的话,只需要在<url-pattern></url-pattern>中写入相对路径就可以了.

所有程序都已经调试没有任何错误!
 
黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(c,bean,jsp,servlet,配置管理)