📄 globalfilter.java
字号:
/*******************************************************************************
* 文件名: GlobalFilter.java <br>
* 版本: <br>
* 描述: 设置系统全局的过滤器类,对页面的访问等操作进行过滤器中设定验证<br>
* 版权所有: <br>
* //////////////////////////////////////////////////////// <br>
* 创建者: 杨赞明 <br>
* 创建日期: 2005-8-18 <br>
* 修改者: <br>
* 修改日期: <br>
* 修改说明: <br>
******************************************************************************/
package com.hope.common.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 javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
* 设置系统全局的过滤器类,对页面的访问等操作进行过滤器中设定验证,验证成功则进入相关页面或类<br>
* 不成功则转向系统信息提示页面,并提示相关信息
*/
public class GlobalFilter implements Filter {
/**
* 字符编码集
*/
protected String encoding = null;
/**
* 过滤器配置对象
*/
protected FilterConfig filterConfig = null;
/**
* 取消过滤标志
*/
protected boolean ignore = true;
//继承Filter类必须实现的方法,本类中设定为空方法
public void init(FilterConfig config) throws ServletException {
System.out.println("@@@@@@@@@@~~~~~~~~~~~~~~~~~@@@@");
// this.filterConfig = filterConfig;
// this.encoding = filterConfig.getInitParameter("encoding");
// String value = filterConfig.getInitParameter("ignore");
// if (value == null)
// this.ignore = true;
// else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
// this.ignore = true;
// else
// this.ignore = false;
}
/**
* 定义过滤方法,通过对request对象中请求的判断,根据不同状态执行不同措施
*
* @param request ServletRequest
* @param response ServletResponse
* @throws IOException
* @throws ServletException
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("@@@@@@@@@@~~~~~~~~~~~~~~~~~111@@@@");
//将接收到的request,response对象转型为HttpServletRequest和HttpServletResponse
HttpServletRequest q = (HttpServletRequest) request;
HttpServletResponse p = (HttpServletResponse) response;
//将得到的url地址转为小写
String url = q.getRequestURI().toLowerCase();
//判断是否是要求检查的url
if ( (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
if (isCheckURL(url)) {
HttpSession session = q.getSession(false);
//判断session是否为空,同时session中loginInfo代表的对象是否存在
if (session == null || session.getAttribute("loginInfo") == null) {
//不存在则创建ActionMessages对象,封装提示信息,转向系统信息提示页面
ActionMessages ams = new ActionMessages();
ams.add("sysMessage",
new ActionMessage("error.session.invalid"));
ams.add("sysInfoBtn", new ActionMessage("btn.value.relogin"));
q.setAttribute("sysInfoLink", "index.do");
q.setAttribute(Globals.MESSAGE_KEY, ams);
//设置转向位置
q.getRequestDispatcher("/error.do").forward(q, p);
}
//通过过滤器检查,转向请求
else {
chain.doFilter(request, response);
}
}
//通过过滤器检查,转向请求
else {
chain.doFilter(request, response);
}
}
//继承Filter类必须实现的方法,本类中设定为空方法
public void destroy() {
}
//继承Filter类必须实现的方法,本类中设定为空方法
public FilterConfig getFilterConfig() {
return null;
}
//继承Filter类必须实现的方法,本类中设定为空方法
public void setFilterConfig(FilterConfig config) {
}
/**
* 获取用户设定的字符集,如没有设定,返回null。
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
/**
* 定义检查url是否是指定不需检查的url地址
*
* @param url String
* @return boolean
*/
private boolean isCheckURL(String url) {
//定义一个不需检查url的数组
String[] uncheckUrl = { "index", "exitaction.do", "loginaction.do",
"error.do", "sysinfo.jsp", };
boolean check = true;
for (int i = 0; (i < uncheckUrl.length && check); i++) {
//判断url是否存在String数组中的String,如果url中不包含String数组则返回值为-1
//所以只有url中包含String数组check才可能为false
if (url.indexOf(uncheckUrl[i]) != -1) {
check = false;
}
}
return check;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -