- 浏览: 403327 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zyu67:
这个类运行不起来呀 这类如何导出数据呀
一个数据库连接Java工具类和数据迁移类 -
kobe7777:
你好,请问我想写个excelToExcel 另存一个excel ...
Jacob 另存为Word、Excel -
di1984HIT:
记录一下学习。
Eclipse 平台架构 -
di1984HIT:
鞋的不错啊。呵呵额
一个数据库连接Java工具类和数据迁移类 -
Jameslyy:
确认jacob dll是否已经放入系统路径,jacob版本不同 ...
Jacob 另存为Word、Excel
1、Servlet Listener
共有8个可以实现的监听器接口,和6种相对应的触发事件,实现在触发不同事件时执行相应的操作。如下
- //
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- //
- import javax.servlet.ServletRequestAttributeEvent;
- import javax.servlet.ServletRequestAttributeListener;
- //
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- //
- import javax.servlet.ServletContextAttributeEvent;
- import javax.servlet.ServletContextAttributeListener;
- //
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import javax.servlet.http.HttpSessionActivationListener;
- //
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionBindingListener;
- import javax.servlet.http.HttpSessionAttributeListener;
1)、javax.servlet.ServletContextListener
Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.
void com.james.webapp.ServletListener.contextInitialized(ServletContextEvent arg0)
Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.
void com.james.webapp.ServletListener.contextDestroyed(ServletContextEvent arg0)
Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any ServletContextListeners are notified of context destruction.
2)、javax.servlet.ServletContextAttributeListener
Implementations of this interface receive notifications of changes to the attribute list on the servlet context of a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.
- /** Notification that a new attribute was added to the servlet context.
- Called after the attribute is added.*/
- public void attributeAdded(ServletContextAttributeEvent scab);
- /** Notification that an existing attribute has been removed from the servlet context.
- Called after the attribute is removed.*/
- public void attributeRemoved(ServletContextAttributeEvent scab);
- /** Notification that an attribute on the servlet context has been replaced.
- Called after the attribute is replaced. */
- public void attributeReplaced(ServletContextAttributeEvent scab);
3)、javax.servlet.ServletRequestListener
A ServletRequestListener can be implemented by the developer interested in being notified of requests coming in and out of scope in a web component. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.
- /** The request is about to go out of scope of the web application. */
- public void requestDestroyed ( ServletRequestEvent sre );
- /** The request is about to come into scope of the web application. */
- public void requestInitialized ( ServletRequestEvent sre );
4)、javax.servlet.ServletRequestAttributeListener
A ServletRequestAttributeListener can be implemented by the developer interested in being notified of request attribute changes. Notifications will be generated while the request is within the scope of the web application in which the listener is registered. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.
Since: Servlet 2.4
- /** Notification that a new attribute was added to the
- ** servlet request. Called after the attribute is added.
- */
- public void attributeAdded(ServletRequestAttributeEvent srae);
- /** Notification that an existing attribute has been removed from the
- ** servlet request. Called after the attribute is removed.
- */
- public void attributeRemoved(ServletRequestAttributeEvent srae);
- /** Notification that an attribute was replaced on the
- ** servlet request. Called after the attribute is replaced.
- */
- public void attributeReplaced(ServletRequestAttributeEvent srae);
5)、javax.servlet.http.HttpSessionListener
Implementations of this interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.
Since: v 2.3
- /**
- * Notification that a session was created.
- * @param se the notification event
- */
- public void sessionCreated ( HttpSessionEvent se );
- /**
- * Notification that a session is about to be invalidated.
- * @param se the notification event
- */
- public void sessionDestroyed ( HttpSessionEvent se
6)、javax.servlet.http.HttpSessionActivationListener
Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.
Since: 2.3
- /** Notification that the session is about to be passivated.*/
- public void sessionWillPassivate(HttpSessionEvent se);
- /** Notification that the session has just been activated.*/
- public void sessionDidActivate(HttpSessionEvent se);
7)、javax.servlet.http.HttpSessionBindingListener
Causes an object to be notified when it is bound to or unbound from a session. The object is notified by an
HttpSessionBindingEvent
object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.See Also: HttpSession HttpSessionBindingEvent
- /**
- * Notifies the object that it is being bound to
- * a session and identifies the session.
- *
- * @param event the event that identifies the session
- */
- public void valueBound(HttpSessionBindingEvent event);
- /**
- * Notifies the object that it is being unbound
- * from a session and identifies the session.
- *
- * @param event the event that identifiesthe session
- */
- public void valueUnbound(HttpSessionBindingEvent event);
8)、javax.servlet.http.HttpSessionAttributeListener
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
Since: v 2.3
- /** Notification that an attribute has been added to a session.
- Called after the attribute is added.*/
- public void attributeAdded ( HttpSessionBindingEvent se );
- /** Notification that an attribute has been removed from a session.
- Called after the attribute is removed. */
- public void attributeRemoved ( HttpSessionBindingEvent se );
- /** Notification that an attribute has been replaced in a session.
- Called after the attribute is replaced. */
- public void attributeReplaced ( HttpSessionBindingEvent se );
2、Servlet Filter
需要实现接口javax.servlet.Filter,下面是一个例子:
- import java.io.IOException;
- 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 ServerletFilter implements Filter {
- public void destroy() {
- // TODO Auto-generated method stub
- }
- public void doFilter(ServletRequest arg0, ServletResponse arg1,
- FilterChain arg2) throws IOException, ServletException {
- // TODO Auto-generated method stub
- }
- public void init(FilterConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
- }
- }
javax.servlet.Filter
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
Filters perform filtering in thedoFilter
method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.Filters are configured in the deployment descriptor of a web application
Examples that have been identified for this design are
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter
void com.james.webapp.ServerletFilter.destroy()
Called by the web container to indicate to a filter that it is being taken out of service. This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter.
This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter's current state in memory.
void com.james.webapp.ServerletFilter.doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException
The
doFilter
method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.A typical implementation of this method would follow the following pattern:-
1. Examine the request
2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
4. a) Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()
),
4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
5. Directly set headers on the response after invocation of the next entity in the filter chain.
void com.james.webapp.ServerletFilter.init(FilterConfig arg0) throws ServletException
Called by the web container to indicate to a filter that it is being placed into service. The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work.
The web container cannot place the filter into service if the init method either
1.Throws a ServletException
2.Does not return within a time period defined by the web container
发表评论
-
Maven Tips
2019-09-18 14:49 4471. 编译 mvn clean package ... -
命令行参数解析 args4j/OptionParser - Java/Python
2017-04-24 11:39 13031. args4j -- Java public ... -
SFTP Java Client -- jcraft.jsch
2017-04-24 11:17 1057http://www.jcraft.com/jsch ... -
OWC 显示和读取Excel数据
2012-03-31 14:29 2530在网页中嵌入Excel: <object id=Spr ... -
Jacob 读写Excel
2012-03-31 14:15 8958Jacob 读写Excel JacobExcelUti ... -
Eval 数学公式解析计算
2011-07-20 22:42 2394Eval http://java.net/projects/ ... -
固定表头
2011-07-05 08:55 1460Grid 资源 FixedHeaderTable htt ... -
JSP相关
2011-02-28 11:18 1614JSP页面获取应用的上下文路径 <%@ tagl ... -
JBoss Seam 资源
2007-12-04 17:54 1500深入浅出JBoss Seam http://www.infoq ... -
Hibernate和Access
2007-09-26 10:15 100321、Hibernate对于Access的支持 方法一: 下载两 ... -
Timer, Quartz 和 Spring 实现作业调度
2007-08-23 15:20 4252一、java.util.Timer 在Java ... -
JSF 资源
2007-08-02 17:58 1716SUN JSF resources http://java.s ...
相关推荐
Servlet、Filter、Listener 详解 Servlet 是运行在服务器上的小程序,它可以使任何 Java 应用程序、...Servlet 负责处理客户端的请求,Filter 负责过滤和拦截 Web 资源,而 Listener 负责监听Servlet 容器中的事件。
一般来说,listener 的加载顺序是在 filter 和 servlet 之前的,因为 listener 需要监听 ServletContext 的事件,以便实现对 Web 应用的监听和控制。filter 的加载顺序是在 servlet 之前的,因为 filter 需要对请求...
关于filter、servlet在web.xml配置及加载顺序
Servlet、Filter和Listener是Java Web开发中的核心组件,它们在构建动态Web应用程序中扮演着重要角色。下面将分别深入解析这三个概念。 **Servlet接口** Servlet是Java编程语言中定义的一个接口,它允许开发者创建...
Web服务器中的三大组件,即Servlet、Filter和Listener,是构建动态Web应用程序的关键元素。这些组件都是基于Java的,主要用于增强和扩展Web服务器的功能。 Servlet是Java中用于处理HTTP请求的核心组件,它是动态...
`chapter02.docx`可能包含的是关于Servlet、Filter和Listener的第二章内容,详细讲解了这三个组件的实现细节、配置方法以及实际应用案例。而`myblog_v2`可能是一个示例项目,展示了如何在实际的博客系统中运用这些...
在IT领域,Listener、Filter和工具是Web开发中不可或缺的部分,它们在构建高效、可扩展的应用程序中扮演着重要角色。下面将详细讲解这三个概念及其相关知识点。 首先,Listener(监听器)是Java Servlet规范中的一...
按照配置顺序,先执行Listener,然后是Filter,接着是Struts拦截器,最后是Servlet。 - Filter的执行顺序取决于它们在web.xml中的配置顺序。每个Filter的doFilter()方法会被调用,直到请求达到Servlet,或者在...
通过Spring管理Filter和Servlet,不仅可以充分利用Spring的依赖注入能力,简化Filter和Servlet的配置,还能增强代码的可维护性和可扩展性。开发者无需在Filter或Servlet内部硬编码bean名称,而是通过Spring容器自动...
Java WEB 篇九 Java servlet、filter、listener、interceptor 之间的区别和联系?
通常JavaWeb项目启动时我们需要... 下面代码是模拟初始化的一个示例,可以在控制台看到程序的输出,和Listener、Filter、Servlet的启动先后顺序,强烈建议跟我一样喜欢动手的Coder操作一下,废话不多说,直接上代码了。
### Filter和Listener在Java Web开发中的应用与差异 在Java Web开发中,Filter(过滤器)和Listener(监听器)是两个重要的概念,它们在Web应用程序的生命周期管理和请求处理流程中扮演着关键角色。理解它们的区别...
监听器是指Servlet API中的一个Listener接口,它允许开发人员监听Servlet的生命周期事件,例如ServletContextListener、ServletRequestListener、HttpSessionListener等。监听器可以实现一些特殊的功能,例如实现...
对于Servlet、Filter和Listener,由于它们通常在Web应用启动时由容器实例化,而非由Spring管理,所以也不能直接使用@Autowired注解或其他常规的注入方式。为了解决这个问题,可以使用以下策略: 1. **Servlet**: ...
本源码将详细介绍web.xml配置中servlet,filter,listener的加载顺序,可以让学习者更好的了解web.xml各种属性配置,自己写的东西,不足之处请大家见谅,顺便收点积分也好下资料,谢谢
在Java Web开发中,Servlet、Filter和Listener是三个核心组件,它们构成了Web应用程序的基础架构,用于处理HTTP请求、实现业务逻辑以及管理应用的生命周期。现在,让我们深入探讨这些概念及其在实际开发中的应用。 ...
### web.xml文件中配置(servlet, spring, filter, listener)的加载顺序 在Java Web应用开发中,`web.xml`文件是整个Web应用程序的核心配置文件之一,它定义了Servlet容器如何启动、初始化以及配置各个组件如...
我们可以使用@WebServlet、@WebFilter、@WebListener注解来配置Servlet、Filter、Listener,并使用@ServletComponentScan注解来扫描这些组件。这样,我们可以轻松地在SpringBoot中使用Servlet、Filter、Listener。
Servlet、Filter和Listener是Java Web开发中的三大核心组件,它们在构建动态Web应用程序时起着至关重要的作用。这个"Servlet小例子源码"压缩包显然提供了关于这些概念的实际应用示例,非常适合初学者来理解和掌握。 ...