`
酷的飞上天空
  • 浏览: 518775 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

简易blog配置的filter介绍

阅读更多

这个blog一共就写了三个filter

AbstractFilter 启动的核心filter,用来进行配置文件的读取与保存,简单连接池的初始化与关闭以及其他内容

BlogFilter 继承自AbstractFilter ,负责字符集过滤和连接的打开与释放

AdminFilter  拦截未登录用户的请求。

 

AbstractFilter 内容如下

public abstract class AbstractFilter implements Filter{
	public static BlogContext blogContext = new BlogContext();
	//网站最近一次启动以来的流量统计
	public static int totalNum = 0;
	@Override
	public void destroy() {
		DataSourceUtil.closePool();
		blogContext.destroyed();
		LogUtil.info(this.getClass().getName(), "数据库连接关闭操作完成");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//每一次有新的session,就使网站计数器加1
		if(WebUtil.getCookie("totalNum",request)==null){
			totalNum++;
			 WebUtil.setCookie("totalNum", ""+totalNum+"", response);
		}
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String xmlFilePath = filterConfig.getServletContext().getRealPath("/WEB-INF/blog.xml");
		blogContext.setXmlHandle(new SimpleXmlHandle(xmlFilePath));
		blogContext.init();
		DataSourceUtil.init(blogContext.getDatabaseUrl(), blogContext.getDatabaseUser(), blogContext.getDatabasePassWord());
		LogUtil.info(this.getClass().getName(), "数据库连接初始化完成.");
		//设置blog的大标题,/include/head.jsp用到。在UpdateUserInfoServlet中可能被修改
		filterConfig.getServletContext().setAttribute("blogTitle", blogContext.getUser().getBlogTitle());
		LogUtil.info(this.getClass().getName(), "博客标题为:"+BlogContext.getInstance().getUser().getBlogTitle());
	}
	public static int getTotalNum(){
		return totalNum;
	}
	public static BlogContext getBlogContext(){
		return blogContext;
	}
}

 从WEB-INF/blog.xml下面读取配置文件,将内容解析到blogContext下,关于blogContext在下篇贴出来,包括xml的简单解析器SimpleXmlHandle

blogContext.setXmlHandle(new SimpleXmlHandle(xmlFilePath)); 设置解析xml的解析器

blogContext.init();进行xml解析,并填充blogContext数据

DataSourceUtil.init(blogContext.getDatabaseUrl(), blogContext.getDatabaseUser(), blogContext.getDatabasePassWord()); 初始化数据库的链接池,从解析好的blogContext中获得连接信息

filterConfig.getServletContext().setAttribute("blogTitle", blogContext.getUser().getBlogTitle());从blogContext中取得标题信息,并设置为blog的标题

 

BlogFilter内容如下

public class BlogFilter extends AbstractFilter {

	private static ThreadLocal<Connection> connHold = new ThreadLocal<Connection>();
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		super.doFilter(request, response, chain);
		try {
			request.setCharacterEncoding("UTF-8");
			chain.doFilter(request, response);
		} finally {
			Connection conn = connHold.get();
			if(conn!=null){
				connHold.remove();
				DataSourceUtil.close(conn);
			}
		}
	}
	public static Connection getConnection(){
		Connection conn = connHold.get();
		if(conn==null){
			conn = DataSourceUtil.getConnection();
			connHold.set(conn);
		}
		return conn;
	}
	
}

 

内容不多说了,很简单。不用注释都能看懂。

 

AdminFilter 内容如下

public class AdminFilter implements Filter {

	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		String isLogin = WebUtil.getValueFromSession("isLogin", req);
		if(isLogin==null){
			((HttpServletResponse)response).sendRedirect(((HttpServletRequest)request).getContextPath()+"/login.jsp");
			return;
		}
		chain.doFilter(request, response);
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

}

 

以当前session是否含有key为isLogin的值为判断依据,有则代表已登录,否则反之。拦截地址为admin/*

 

 


 

分享到:
评论

相关推荐

    jfinalpluginsjfinal-dreampie.zip

    https://github.com/Dreampie?tab=repositories 分割成多个独立的插件进行优化,可以看我的博客拆分优化情况 http://my.oschina.net/wangrenhui1990/blog demo:(Angularjs前端 jfinal-dreampie插件) ...

    asp.net知识库

    技术基础 New Folder ...知识集锦:三分钟全面了解 Blog 和 RSS C#+ASP.NET开发基于Web的RSS阅读器 ASP.NET RSS Toolkit(RSS工具) Serialize Your Deck with Positron [XML Serialization, XSD, C#]...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK开发范例大全的目录

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google+Android+SDK开发范例大全

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    4.20 以动态列表配置选项——ListActivity与Menu整合技巧 4.21 查找程序根目录下所有文件——JavaI/O与ListActivity的结合.. 4.22 加载手机磁盘里的图文件——使用decodeFile方法 4.23 动态放大缩小ImageView里的...

    Google Android SDK 开发范例大全01

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK 开发范例大全02

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android sdk 开发范例大全 部分章节代码

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

    Google Android SDK开发范例大全(完整版)

    3.7 简易的按钮事件——Button事件处理 3.8 手机页面的转换——setContentView的应用 3.9 调用另一个Activity——Intent对象的使用 3.10 不同Activity之间的数据传递——Bundle对象的实现 3.11 返回数据到前一个...

Global site tag (gtag.js) - Google Analytics