Android Development. Game Development. GO Language. Spring Framework. Go to Tutorials Library. Interactive Courses, where you Learn by doing. Available for FREE!
Q Which statement about listeners are not true? A ServletResponseListener can be used to perform an action when servlet response has been sent. A servlet container listener can be used to perform an action when servlet context is about to be shut down.
When declaring a listener in the deployment descriptor, which sub-elements of the element are required? Use this method to get the value of the attribute that was added, removed, or replaced. In the case of an attribute that was replaced, this method returns the old value, not the new value. The HttpSessionEvent class includes the following method, which your listener can call:. Use this method to retrieve the session object that was created or destroyed, from which you can obtain information as desired.
See "Introduction to Servlet Sessions" for information about the javax. HttpSession interface. The servlet container calls this method to notify the listener that an attribute was added to the session. The servlet container calls this method to notify the listener that an attribute was removed from the session. The servlet container calls this method to notify the listener that an attribute was replaced in the session. The HttpSessionBindingEvent class includes the following methods, which your listener can call:.
This section provides code for a sample that uses a servlet context lifecycle and session lifecycle event listener. This includes the following components:. To download and run this application, refer to the Use Servlet Lifecycle Events example at the following link:. These examples were written for older versions of OC4J, but the functionality is similar for the Here is the welcome page, the user interface that enables you to invoke the session-creation servlet by clicking the Create New Session link, or to invoke the session-destruction servlet by clicking the Destroy Current Session link.
The servlets and the event listener are declared in the web. This results in SessionLifeCycleEventExample being instantiated and registered upon application startup. Because of this, the servlet container automatically calls its methods, as appropriate, upon the occurrence of servlet context or session lifecycle events. Here are the web. This section shows the listener class. Its sessionCreated method is called by the servlet container when an HTTP session is created, which occurs when you click the Create New Session link in index.
This servlet is invoked when you click the Create New Session link in index. Its invocation results in the servlet container creating a request object and associated session object.
Creation of the session object results in the servlet container calling the sessionCreated method of the event listener class. This servlet is invoked when you click the Destroy Current Session link in index. Its invocation results in a call to the invalidate method of the session object. This, in turn, results in the servlet container calling the sessionDestroyed method of the event listener class.
Skip Headers. The methods are: init destroy doFilter. How the Ser vlet Container Invokes Filters Figure shows how the servlet container invokes filters. Note: Be careful in coordinating any use of multiple filters, in case of possible overlap in functionality or in what the filters are overwriting. You can change this behavior, however, through the following environment setting: oracle.
Note: This flag is a temporary mechanism for the current release. Future releases will adhere to version 2. But in compliance with the specification, you can configure this behavior through the web.
Filter Examples This section lists and describes three servlet filter examples. Filter Ex ample 1 This section provides a simple filter example. Gen eric Filter Here is the generic filter code. IOException, javax.
The numbered code notes refer to the following: This code declares a variable to save the filter configuration object.
The doFilter method contains the code that implements the filter. In the generic case, just call the filter chain. The init method saves the filter configuration in a variable. The destroy method can be overridden to accomplish any required finalization. Filter Co de: HelloWorldFilter. JSP Co de: filter. Running Example 1 Invoke filter. Filter Exa mple 2 You can configure a filter with initialization parameters in the web. JSP Code: filter2.
Running Example 2 Verify that the filter configuration has been entered in the web. Entering MessageFilter Exiting MessageFilter Note the message from the server showing that it redeployed the default Web application after the web. Filter Exa mple 3 A particularly useful function for a filter is to manipulate the response to a request. Create three new classes to implement this example: FilterServletOutputStream : This class is a new implementation of ServletOutputStream for response wrappers.
PrePostFilter : This class implements the filter. Here is the code for the new output stream: package com. The GenericResponseWrapper class accomplishes this: package com. Writing the Filter This filter adds content to the response after that target is invoked. Here is the filter code, PrePostFilter.
Running Example 3 Invoke the servlet in your Web browser. Event Listeners The servlet specification includes the capability to track key events in your Web applications through event listeners. Session-level event This event involves resources or state associated with the series of requests from a single user session; that is, associated with the HTTP session object. Each of these two levels has two event categories: Lifecycle changes Attribute changes You can create one or more event listener classes for each of the four event categories.
Table Event Listener Categories and Interfaces Event Category Event Descriptions Java Interface Servlet context lifecycle changes Servlet context creation, at which point the first request can be serviced Imminent shutdown of the servlet context javax. ServletContextListener Servlet context attribute changes Addition of servlet context attributes Removal of servlet context attributes Replacement of servlet context attributes javax. HttpSessionListener Session attribute changes Addition of session attributes Removal of session attributes Replacement of session attributes javax.
Typic al Event Listener Scenario Consider a Web application comprising servlets that access a database. This listener may function as follows: The listener is notified of application startup. The application logs in to the database and stores the connection object in the servlet context. Servlets use the database connection to perform SQL operations. Prior to application shutdown, the listener closes the database connection.
Eve nt Listener Declaration and Invocation Event listeners are declared in the application web. Eve nt Listener Coding and Deployment Guidelines Be aware of the following rules and guidelines for event listener classes: In a multithreaded application, attribute changes may occur simultaneously. Each listener class must have a public zero-argument constructor. Note: In a distributed environment, the scope of event listeners is one for each deployment descriptor declaration for each JVM.
There is no requirement for distributed Web containers to propagate servlet context events or session events to additional JVMs. The servlet specification discusses this. We look at the option to create a c ustom HttpSessionListner along with details as when we should think about creating a custom session listener for our application.
As always, the source code for this post is available on the GitHub. Adding Session to the counter. Removing the Session from the counter.
If we run our application, you might see similar output: SessionDemoController : Unable to find session.
Creating a new session CustomSessionListner : it creates New session. CustomSessionListner : Total active session are 1
0コメント