标签:servlet
一:filter过滤器
1.简单使用
public classSimpleFilter extends HttpServlet implements Filter {
public void init(FilterConfig config)throws ServletException {
String initParm=config.getInitParameter("ref");
System.out.println("初始化过滤器:"+initParm);
}
public void doFilter(ServletRequest arg0,ServletResponse arg1,
FilterChain chain) throwsIOException, ServletException {
System.out.println("****执行doFilter()方法前****");
chain.doFilter(arg0, arg1);
System.out.println("****执行doFilter()方法后****");
}
@Override
public void destroy() {
System.out.println("销毁过滤器");
}
}
web.xml下配置过滤器
<filter>
<filter-name>SimpleFilter</filter-name>
<filter-class>com.cloud.day1.SimpleFilter</filter-class>
<init-param>
<param-name>ref</param-name>
<param-value>Spring</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SimpleFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.过滤编码
http://blog.csdn.net/dzy21/article/details/47042891
该文章详细使用了过滤器解决编码问题。
二:监听器
public class Demo2implements ServletContextListener {
public void contextInitialized(ServletContextEventevent) {
System.out.println("初始化:"+event.getServletContext().getContextPath());
}
public voidcontextDestroyed(ServletContextEvent event) {
System.out.println("销毁:"+event.getServletContext().getContextPath());
}
}
web.xml下配置监听器
<!-- 配置监听器 -->
<listener>
<listener-class>
com.cloud.day1.Demo2
</listener-class>
</listener>
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:servlet
原文地址:http://blog.csdn.net/dzy21/article/details/47321391