Filter應用+struts2x攔截器區別
來源:互聯網
上載者:User
(1)Filter與struts2x攔截器區別:Filter:當程式請求servlet,jsp時,Filter會進行攔截。程式將先經過filter後,才到達目標的servlet,jsp;常用於系統許可權管理(即使用者訪問某些頁面之前,進行Filter許可權檢查)struts2x攔截器:只是在程式訪問Action之前進行攔截。常用於記錄系統動作記錄,或添加額外功能。 (2)Filter簡介:Filter的三個方法:init(),destroy(),doFilter(); 三個執行個體如下:(1)-------<利用filter進行中文字元處理>------------
1)
在web.xml中的配置:
<filter>
<filter-name>encoding</filter-name>
<filter-class>
org.lxh.myzngt.filter.EncodingFilter
</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>2)
JAVA代碼:
public class EncodingFilter implements Filter {
private String charset = null;public void destroy() {}public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding(this.charset);
chain.doFilter(req, resp);
}//Filter擷取web.xml中的初始參數;public void init(FilterConfig arg0) throws ServletException {
this.charset = arg0.getInitParameter("charset");
}
} (2)<利用filter進行使用者登入過濾>------------
1)
<filter>
<filter-name>userlogin</filter-name>
<filter-class>
org.lxh.myzngt.filter.UserLoginFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>userlogin</filter-name>
<url-pattern>/jsp/user/*</url-pattern>
</filter-mapping>
<filter-mapping> -->可以多個匹配!!!!
<filter-name>userlogin</filter-name>
<url-pattern>/jsp/ques/*</url-pattern>
</filter-mapping>
2)
public class UserLoginFilter implements Filter {
public void destroy() {
}public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) arg0;
if (req.getSession().getAttribute("userid") != null) {
arg2.doFilter(arg0, arg1);
} else {
req.getRequestDispatcher("forward.htm").forward(arg0, arg1) ;
}
}public void init(FilterConfig arg0) throws ServletException {
}
} (3)在使用者訪問目標頁面之前,利用Filter進行進行許可權查詢:1.使用者是否已登入(即session中是否有相應的記錄)2.使用者權限是否符合訪問頁面要求。如下:(1)在web.xml中添加:
<filter>
<filter-name>PrivilegeFilter</filter-name>
<filter-class>com.demo.web.filter.PrivilegeFilter</filter-class>
<init-param>
<param-name>doNotFilterURL</param-name>
<param-value>
admin/rand, /login, admin/logout, /common/, /images/, /style/, /js/, /fckeditor/, /userFilesAbsolutePath, /editPass
</param-value>
</init-param>
</filter><filter-mapping>
<filter-name>PrivilegeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> (2)
com.demo.web.filter.PrivilegeFilter定義:
public class PrivilegeFilter implements Filter {/** 不需要過濾的URL */
private String[] doNotFilterURL;public void init(FilterConfig filterConfig) throws ServletException {
String params = filterConfig.getInitParameter("doNotFilterURL");
if (params != null) {
String urls[] = params.split(",");
doNotFilterURL = new String[urls.length];
for (int i = 0, size = urls.length; i < size; i++) {
doNotFilterURL[i] = urls[i];
}
}
}
HttpServletRequest req = (HttpServletRequest) request;
String requestPath = req.getRequestURI(); //如:demo/login.action
String contextRoot = req.getContextPath(); //如:demo int length = contextRoot.length();
String path = requestPath.substring(length); //如:/login.action
if (path != null && path.length() != 0) {
path = path.trim();
} /** 登入頁面不進行攔截 */
if (Constants.FIRST_LOGIN_URL.getStringValue().equals(path)) {
return true;
}
//擷取請求的地址,比對不需要過濾的URL的數組doNotFilterURL。
boolean doNotFilter = false;
if (doNotFilterURL != null) {
for (String url : doNotFilterURL) { if (url != null && path.contains(url.trim())) {
doNotFilter = true;
break;
}
}
} //對不屬於不用過濾的,查詢資料表,看使用者是否有權訪問。若沒,則返回提示使用者無限訪問頁面。若有,則直接通過。