当前位置:首页 > 开发 > 互联网 > 正文

Listener Filter Servlet回顾

发表于: 2011-10-25   作者:antlove   来源:转载   浏览次数:
摘要: 这是简单的案例,代码如下: Listener: import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public cla
这是简单的案例,代码如下:
Listener:
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("Listener Destory");
	}

	@SuppressWarnings("unchecked")
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("Listener Initialize");
		ServletContext servletContext = sce.getServletContext();
		Enumeration<String> names = servletContext.getInitParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = servletContext.getInitParameter(name);
			System.out.println("param name : " + name + ",param value : " + value);
		}
	}

}


Filter:
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter {

	public void destroy() {
		System.out.println("Filter destory");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		chain.doFilter(request, response);
	}

	@SuppressWarnings("unchecked")
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("Filter init");
		Enumeration<String> names = filterConfig.getInitParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = filterConfig.getInitParameter(name);
			System.out.println("param name : " + name + ",param value : " + value);
		}
	}

}


Servlet:
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class MyServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		doPost(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Enumeration<String> names=this.getInitParameterNames();
		while(names.hasMoreElements()){
			String name=names.nextElement();
			String value=this.getInitParameter(name);
			System.out.println("param name : " + name + ",param value : " + value);
		}
	}
}


web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
      <param-name>servlet context param</param-name>
      <param-value>servlet容器值</param-value>
  </context-param>
  
  <filter>
     <filter-name>myFilter</filter-name>
     <filter-class>MyFilter</filter-class>
     <init-param>
        <param-name>filter param</param-name>
        <param-value>过滤器值</param-value>
     </init-param>
  </filter>
  
  <filter-mapping>
      <filter-name>myFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

  <servlet>
     <servlet-name>myServlet</servlet-name>
     <servlet-class>MyServlet</servlet-class>
     <init-param>
        <param-name>servlet param</param-name>
        <param-value>servlet初始值</param-value>
     </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
  </servlet-mapping>
  
  <listener>
  	<listener-class>MyListener</listener-class>
  </listener>
</web-app>

Listener Filter Servlet回顾

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Java Servlet是与平台无关的服务器端组件,运行于Servlet容器中(如Tomcat),Servlet容器负责Servlet
Java Servlet是与平台无关的服务器端组件,运行于Servlet容器中(如Tomcat),Servlet容器负责Servlet
一、概念: 1、servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,
一、概念: 1、servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,
由于最近两个月工作比较清闲,个人也比较“上进”,利用工作空余时间,也继续学习了一下,某天突然
下面从几个方面阐述一下题目中四个概念的区别与联系: 1、概念 2、生命周期 3、职责 4、执行过程 一
由于最近两个月工作比较清闲,个人也比较“上进”,利用工作空余时间,也继续学习了一下,某天突然
由于最近两个月工作比较清闲,个人也比较“上进”,利用工作空余时间,也继续学习了一下,某天突然
原文地址:http://blog.csdn.net/sundenskyqq/article/details/8549932 由于最近两个月工作比较清闲
下面从几个方面阐述一下题目中四个概念的区别与联系: 1、概念 2、生命周期 3、职责 4、执行过程 一
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号