?? signonfilter.java
字號:
package com.jspdev.filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpSession;
public class SignonFilter implements Filter
{
String LOGIN_PAGE="/login";
protected FilterConfig filterConfig;
//過濾處理的方法
public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException
{
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin="";
try
{
isLogin=(String)session.getAttribute("isLogin");
if(isLogin.equals("1"))
{
//驗證成功,繼續(xù)處理
chain.doFilter(req,res);
}
else
{
//驗證不成功,讓用戶登錄。
hres.sendRedirect(LOGIN_PAGE);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
//銷毀過濾器
public void destroy()
{
this.filterConfig=null;
}
/**
*初始化過濾器,和一般的Servlet一樣,它也可以獲得初始參數(shù)。
*/
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -