servlet学习总结四

在javaWeb中,我们会用到过滤器 监听器和拦截器 。

过滤器其实是一个典型的过滤链,主要用于对request做预处理和可以对response做结果处理

public class UserFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("servlet Filter init");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("this servlet filter");
		
		// 把请求传回过滤链
      	chain.doFilter(request,response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("servlet filter destroy");
	}
	
	
}
web.xml

    <!-- servlet 过滤器 -->
    <filter>
	<filter-name>MyFilter</filter-name>
	<filter-class>demo.com.cn.ssm.filter.UserFilter</filter-class>		
    </filter>
    <filter-mapping>
    	<filter-name>MyFilter</filter-name>
    	<!-- 拦截所有 请求 -->
		<url-pattern>/*</url-pattern>
    </filter-mapping>
只需实现javax.servlet.Filter接口,并在web.xml配置即可

servlet监听器是事件监听器,是基于事件的。

/**
 * 监听 servletContext 创建和销毁
 * <p>标题: </p>
 * <p>描述: </p>
 * @autho zx
 * @time 2016年9月18日 上午11:13:05
*/
public class MyListener implements ServletContextListener{

	/**
	 * 在servletContext 创建的时候触发(启动应用)
	 * (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 * @autho zx
	 * @time 2016年9月18日 上午11:03:52
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("this contextInitialized ~~");
		ServletContext servletContext = sce.getServletContext();
		servletContext.setAttribute("name1", "n1");
		servletContext.setAttribute("name2", "n2");
	}

	/**
	 * 在servletConfig 销毁的时候触发(关闭应用)
	 * (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 * @autho zx
	 * @time 2016年9月18日 上午11:04:14
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("this  contextDestroyed ~~");
		ServletContext servletContext = sce.getServletContext();
		servletContext.removeAttribute("name1");
		servletContext.removeAttribute("name2");
	}
/**
 * 监听对servletContext 属性操作	 (删除 增加  修改)
 * <p>标题: </p>
 * <p>描述: </p>
 * @autho zx
 * @time 2016年9月18日 上午11:16:56
*/
public class MyListenerAttri implements ServletContextAttributeListener{

	/**
	 * servletContext 增加属性的时候触发
	 * (non-Javadoc)
	 * @see javax.servlet.ServletContextAttributeListener#attributeAdded(javax.servlet.ServletContextAttributeEvent)
	 * @autho zx
	 * @time 2016年9月18日 上午11:18:40
	 */
	@Override
	public void attributeAdded(ServletContextAttributeEvent event) {
		System.out.println("this  attributeAdded");
		System.out.println(event.getName() + ":" + event.getValue());
	}

	/**
	 * servletContext 删除属性的时候触发
	 * (non-Javadoc)
	 * @see javax.servlet.ServletContextAttributeListener#attributeRemoved(javax.servlet.ServletContextAttributeEvent)
	 * @autho zx
	 * @time 2016年9月18日 上午11:19:17
	 */
	@Override
	public void attributeRemoved(ServletContextAttributeEvent event) {
		System.out.println("this attributeRemoved");
		System.out.println(event.getName()+":"+event.getValue());
	}
	
	/**
	 * ServletContext 修改属性的时候触发
	 * (non-Javadoc)
	 * @see javax.servlet.ServletContextAttributeListener#attributeReplaced(javax.servlet.ServletContextAttributeEvent)
	 * @autho zx
	 * @time 2016年9月18日 上午11:19:38
	 */
	@Override
	public void attributeReplaced(ServletContextAttributeEvent event) {
		System.out.println("attributeReplaced ");
		System.out.println(event.getName()+":"+event.getValue());
	}
web.xml

<!-- ServletContext 监听 -->
    <listener>
    	<listener-class>demo.com.cn.ssm.listener.MyListener</listener-class>
    </listener>
    <!-- ServletContext属性 监听 -->
    <listener>
		<listener-class>demo.com.cn.ssm.listener.MyListenerAttri</listener-class>    
    </listener>

servletAPI提供了一些接口,用来监听不同的事件

ServletRequestListener    用于监控 request作用域对象 的创建和销毁
HttpSessionListener    用于监控 session作用域对象 的创建和销毁
ServletContextListener    用于监控 context作用域对象 的创建和销毁
ServletRequestAttributeListener    用于监控 request命名属性 的增、删、改
HttpSessionAttributeListener    用于监控 session命名属性 的增、删、改
ServletContextAttributeListener    用于监控 context命名属性 的增、删、改

拦截器是基于AOP方面的,是基于java反射机制,拦截器不在web.xml中,不同的框架有自己的拦截器,例如springMVC Struts2。

在web.xml,他们初始化的顺序,

org.springframework.web.context.support.ServletContextScope:org.springframework.web.context.support.ServletContextScope@95c19f0
[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties]
[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
org.springframework.web.context.WebApplicationContext.ROOT:Root WebApplicationContext: startup date [Sun Sep 18 14:18:49 CST 2016]; root of context hierarchy
[org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 1731 ms
  this contextInitialized ~~
九月 18, 2016 2:18:50 下午 org.apache.catalina.util.SessionIdGenerator createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [304] milliseconds.
servlet Filter init
初始化 servlet实例
初始化 servlet实例
九月 18, 2016 2:18:50 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\docs
九月 18, 2016 2:18:50 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\docs has finished in 33 ms
九月 18, 2016 2:18:50 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\examples
九月 18, 2016 2:18:51 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
九月 18, 2016 2:18:51 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\examples has finished in 436 ms
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\host-manager
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\host-manager has finished in 37 ms
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\manager
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\manager has finished in 29 ms
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\ROOT
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\ROOT has finished in 22 ms
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\apache-tomcat-8.0.8\webapps\temp
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\apache-tomcat-8.0.8\webapps\temp has finished in 19 ms
九月 18, 2016 2:18:51 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-7777"]
九月 18, 2016 2:18:51 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
九月 18, 2016 2:18:51 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 6832 ms
是先创建Listener,之后是filter,最后才开始创建Servlet实例。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值