1.过滤器
Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码、 做一些业务逻辑判断等。其工作原理是,只要你在web.xml文件配置好要拦截的客户端请求,它都会帮你拦截到请求,此时你就可以对请求或响应(Request、Response)统一设置编码,简化操作;同时还可进行逻辑判断,如用户是否已经登陆、有没有权限访问该页面等等工作。它是随你的web应用启动而启动的,只初始化一次, 以后就可以拦截相关请求,只有当你的web应用停止或重新部署的时候才销毁
主要目的:过滤字符编码;其次,做一些应用逻辑判断等. Filter跟web应用一起启动 当web应用重新启动或销毁时,Filter也被销毁
public class MyCharsetFilter implements Filter {
private FilterConfig config = null;
public void destroy() {
System.out.println("MyCharsetFilter准备销毁...");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
// 强制类型转换
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
// 获取web.xm设置的编码集,设置到Request、Response中
request.setCharacterEncoding(config.getInitParameter("charset"));
response.setContentType(config.getInitParameter("contentType"));
response.setCharacterEncoding(config.getInitParameter("charset"));
// 将请求转发到目的地
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
System.out.println("MyCharsetFilter初始化...");
}
}
<!-- 以下是 MyCharsetFilter.java 在web.xml 中配置: -->
<filter>
<filter-name>filter</filter-name>
<filter-class>dc.gz.filters.MyCharsetFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>contentType</param-name>
<param-value>text/html;charset=UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<!-- * 代表截获所有的请求 或指定请求/test.do /xxx.do -->
<url-pattern>/*</url-pattern>
</filter-mapping>
2.监听器
Servlet容器提供了多种监听器的接口,使用的时候根据需求选择特定的接口实现就行。这些接口都是继承的java.util包中的EventListener接口。
可以看出,监听器的种类确实很多。
常用的有:
(1)request对象的监听ServletRequestAttributeListener和ServletRequestListener
ServletRequestListener用于监听用户请求的创建和销毁。
ServletRequestAttributeListener则用于监听ServletRequest(request)范围内属性的变化,实现该接口的监听器需要实现attributeAdded、attributeRemoved、attributeReplaced三个方法,分别监听Request的增加,删除和修改。
(2)Session对象的监听:HttpSessionAttributeListener和HttpSessionListener
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发sessionCreated(SessionEvent se)方法;当销毁一个 Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发 attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发 attributeReplaced(HttpSessionBindingEvent se) 方法。
(3)ServletContext对象的监听:ServletContextAttributeListener和ServletContextListener。
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加/删除/修改
ServletContextListener监听ServletContext,当创建ServletContext时,激发 contextInitialized (ServletContextEvent sce)方法;当销毁ServletContext时,激发 contextDestroyed(ServletContextEvent sce)方法。
监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。主要作用是: 做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。下面利用监听器对数据库连接池DataSource的初始化演示它的使用:
public class MyServletContextListener implements ServletContextListener {
// 应用监听器的销毁方法
public void contextDestroyed(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
// 在整个web应用销毁之前调用,将所有应用空间所设置的内容清空
sc.removeAttribute("dataSource");
System.out.println("销毁工作完成...");
}
// 应用监听器的初始化方法
public void contextInitialized(ServletContextEvent event) {
// 通过这个事件可以获取整个应用的空间
// 在整个web应用下面启动的时候做一些初始化的内容添加工作
ServletContext sc = event.getServletContext();
// 设置一些基本的内容;比如一些参数或者是一些固定的对象
// 创建DataSource对象,连接池技术 dbcp
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/hibernate");
bds.setUsername("root");
bds.setPassword("root");
bds.setMaxActive(10);// 最大连接数
bds.setMaxIdle(5);// 最大管理数
// bds.setMaxWait(maxWait); 最大等待时间
// 把 DataSource 放入ServletContext空间中,
// 供整个web应用的使用(获取数据库连接)
sc.setAttribute("dataSource", bds);
System.out.println("应用监听器初始化工作完成...");
System.out.println("已经创建DataSource...");
}
}
<!-- web.xml中配置如下,很简单: -->
<!-- 配置应用监听器 -->
<listener>
<listener-class>dc.gz.listeners.MyServletContextListener</listener-class>
</listener>
这样配置好了之后,以后在web应用中就可以通过ServletContext取得BasicDataSource对象,从而获取与数据库的连接,提高性能,方便使用。
3.拦截器
拦截器是在面向切面编程中应用的,就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法。是基于JAVA的反射机制。