SlideShare una empresa de Scribd logo
1 de 16
Implementing Listeners
Agenda
• Understanding Listener
• Monitoring creation and destruction in ServletContext
• Recognizing session creation and destruction
• Recognizing request creation and destruction
Understanding Listeners
• JSP is a template page technology
- High level abstraction of Servlets
• Separation of presentation from logic
• Even non java programmer can create JSP pages with reasonable ease
Available Listeners
• Servlet context listeners.
- These listeners are notified when the servlet context (i.e.,the
Web application) is initialized and destroyed.
• Servlet context attribute listeners.
- These listeners are notified when attributes are added
to,removed from, or replaced in the servlet context.
• Session listeners.
- These listeners are notified when session objects are
created, invalidated, or timed out.
• Session attribute listeners.
- These listeners are notified when attributes are added to,
removed from, or replaced in any session.
Creating a Listeners
• Implement the appropriate interface.
- Use ServletContextListener, ServletContextAttributeListener,
- HttpSessionListener, or HttpSessionAttributeListener.
• Override the methods needed to respond to the events of interest.
- Provide empty bodies for the other methods in the interface.
• Access the important Web application objects.
- Six objects that you are likely to use in event-handling methods:
• The servlet context
• The name of the servlet context attribute that changed
• The value of the servlet context attribute that changed
• The session object
• The name of the session attribute that changed
Creating a Listeners
• Use these objects.
- This process is application specific, but there are some common themes.
For example, with the servlet context, you are most likely to read
initialization parameters getInitParameter), store data for later access
(setAttribute), and read previously stored data (getAttribute).
• Declare the listener.
- You do this with the listener and listener-class elements of the general
Web application deployment descriptor (web.xml) or of a tag library
descriptor file.
• Provide any needed initialization parameters.
- Servlet context listeners commonly read context initialization
parameters to use as the basis of data that is made available to all servlets
and JSP ages. You use the context-param web.xml element to provide the
Monitoring Creation and Destruction
• The ServletContextListener class responds to the Initialization
and destruction of the servlet context.
- These events correspond to the creation and
shutdown of the Web application itself.
• ServletContextListener is most commonly used to
- Set up application-wide resources like database
connection pools
- Read the initial values of application-wide data that
will be used by multiple servlets and JSP pages.
Implementing ServletContextListener
• Implement the ServletContextListener interface.
• Override contextInitialized and contextDestroyed.
- contextInitialized is triggered when the Web application is first loaded and the
servlet context is created. Most common tasks:
• Creating application-wide data (e.g., by reading context init params)
• Storing that data in an easily accessible location .
- contextDestroyed is triggered when the Web application is being shut down
and the servlet context is about to be destroyed. Most common task:
• Releasing resources (e.g. closing connections).
• Obtain a reference to the servlet context.
- The contextInitialized and contextDestroyed methods each take a
ServletContextEvent as an argument.
- The ServletContextEvent class has a getServletContext method that returns the servlet context
Implementing ServletContextListener
• Use the servlet context.
- Read initialization parameters: getInitParameter
- Store data:setAttribute
- Make log file entries: log.
• Declare the listener.
<listener>
<listener-class>package.Listener</listener-class>
</listener>
• Provide needed initialization parameters.
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>
Implementing ServletContextAttributeListener
• Implement ServletContextAttributeListener
• Override attributeAdded, attributeReplaced, and attributeRemoved.
- attributeAdded is triggered when a new attribute name is first added to the
servlet context.
- attributeReplaced is triggered when a new value is assigned to an existing
name. attributeAdded is not triggered in this case. The old value is
obtained via event.getValue and the new value is obtained via context.
- getAttribute. attributeRemoved is triggered when a servlet context attribute
is removed altogether.
• Obtain references to the attribute name, attribute value, and servlet
context.
- Call the following methods of the event object: getName,getValue, and
getServletContext
Implementing
ServletContextAttributeListener
• Use the objects.
- You normally compare attribute name to a stored name to see if it is the one you are
monitoring. The attribute value is used in an application-specific manner. The
servlet context is usually used to read previously stored attributes (getAttribute),
store new or changed attributes (setAttribute), and make entries in the log file (log).
• Declare the listener.
- Use the listener and listener-class elements to list the fully qualified name of the
listener class,
<listener>
<listener-class>
somePackage.SomeListener
</listener-class>
</listener>
Recognizing Session Creation and destruction
• Implement the HttpSessionListener interface.
• Override sessionCreated and sessionDestroyed.
- sessionCreated is triggered when a new session is created.
- sessionDestroyed is triggered when a a session is destroyed. This destruction
could be due to an explicit call to the invalidate method or because the elapsed
time since the last client access exceeds the session timeout.
- Multithreaded access is possible. Synchronize if necessary.
• Obtain a reference to the session and possibly to the servlet context.
- Each of the two HttpSessionListener methods takes an HttpSessionEvent as
an argument. The HttpSessionEvent class has a getSession method that
provides access to the session object.You almost always want this reference;
you occasionally also want a reference to the servlet context. If so, first obtain
the session object and then call getServletContext on it
Recognizing Session Creation and destruction
• Use the objects.
- One of the only methods you usually call on the session is setAttribute. Do this in
sessionCreated if you want to guarantee that all sessions have a certain attribute.
- Wait! What about getAttribute? Nope. In sessionCreated, there is nothing in the session
yet, so getAttribute is pointless. In addition, all attributes are removed before
sessionDestroyed is called, so calling getAttribute is also pointless there. If you want to
clean up attributes that are left in sessions that time out, you use the attributeRemoved
method of HttpSessionAttributeListener. So, sessionDestroyed is mostly reserved for
listeners that are simply keeping track of the number of sessions in use.
• Declare the listener.
- In web.xml or the TLD file, use listener and listener-class to list fully qualified name of
listener class, as below.
<listener>
<listener-class>package.SomeListener</listener-class>
</listener>
Using HttpSessionAttributeListener
• Implement HttpSessionAttributeListener.
• Override attributeAdded, attributeReplaced, and attributeRemoved.
- attributeAdded is triggered when a new attribute name is first added to a
session.
- attributeReplaced is triggered when a new value is assigned to an
existing name. attributeAdded is not triggered in this case. The old value
is obtained via event.getValue and the new value is obtained via
session.getAttribute.
- attributeRemoved is triggered when a session attribute is removed
altogether. This removal can be due to an explicit programmer call to
removeAttribute, but is more commonly due to the system removing all
attributes of sessions that are about to be deleted because their timeout
expired.
Using HttpSessionAttributeListener
• Obtain references to the attribute name, attribute value, session, & ServletContext.
- The HttpSessionAttributeListener methods take an HttpSessionBindingEvent as
args. HttpSessionBindingEvent has three useful methods: getName (name of
attribute that was changed), getValue (value of changed attribute—new value for
attributeAdded and previous value for attribute Replaced and attributeRemoved),
and getSession (the HttpSession object). If you want access to the servlet context,
first obtain the session and then call getServletContext on it.
• Use the objects.
- The attribute name is usually compared to a stored name to see if it is the one you
are monitoring. The attribute value is used in an application-specific manner. The
session is usually used to read previously stored attributes (getAttribute) or to store
new or changed attributes (setAttribute).
• Declare the listener.
- Use listener and listener-class in web.xml as before. `
Summary of Listeners
- Servlet context listeners.
• Notified when servlet context is initialized and destroyed.
- Servlet context attribute Listeners.
• Notified when context attributes are added/removed/replaced
- Session listeners.
• Notified when sessions are created, invalidated, or timed out.
- Session attribute listeners.
• Notified when session attributes are added/removed/replaced

Más contenido relacionado

La actualidad más candente

Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadWASdev Community
 
Lecture 3: Servlets - Session Management
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session ManagementFahad Golra
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Dropwizard Internals
Dropwizard InternalsDropwizard Internals
Dropwizard Internalscarlo-rtr
 
Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Kasun Madusanke
 
Akka Actor presentation
Akka Actor presentationAkka Actor presentation
Akka Actor presentationGene Chang
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycleDhruvin Nakrani
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupRoy Russo
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packagesvamsi krishna
 
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterKonstantin Tsykulenko
 
Akka Cluster in Production
Akka Cluster in ProductionAkka Cluster in Production
Akka Cluster in Productionbilyushonak
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletFahmi Jafar
 
REEF: Towards a Big Data Stdlib
REEF: Towards a Big Data StdlibREEF: Towards a Big Data Stdlib
REEF: Towards a Big Data StdlibDataWorks Summit
 

La actualidad más candente (20)

Servlets
ServletsServlets
Servlets
 
Servlet
Servlet Servlet
Servlet
 
Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 Instead
 
Lecture 3: Servlets - Session Management
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session Management
 
Java servlets
Java servletsJava servlets
Java servlets
 
Dropwizard Internals
Dropwizard InternalsDropwizard Internals
Dropwizard Internals
 
Servlets
ServletsServlets
Servlets
 
SERVIET
SERVIETSERVIET
SERVIET
 
Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0
 
Gradle - Build System
Gradle - Build SystemGradle - Build System
Gradle - Build System
 
Akka Actor presentation
Akka Actor presentationAkka Actor presentation
Akka Actor presentation
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Introduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users GroupIntroduction to Akka - Atlanta Java Users Group
Introduction to Akka - Atlanta Java Users Group
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka Cluster
 
Akka Cluster in Production
Akka Cluster in ProductionAkka Cluster in Production
Akka Cluster in Production
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
REEF: Towards a Big Data Stdlib
REEF: Towards a Big Data StdlibREEF: Towards a Big Data Stdlib
REEF: Towards a Big Data Stdlib
 

Similar a Advance java session 18

SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4Ben Abdallah Helmi
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11Smita B Kumar
 
Advance java session 19
Advance java session 19Advance java session 19
Advance java session 19Smita B Kumar
 
Servlets - filter, listeners, wrapper, internationalization
Servlets -  filter, listeners, wrapper, internationalizationServlets -  filter, listeners, wrapper, internationalization
Servlets - filter, listeners, wrapper, internationalizationsusant sahu
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsPawanMM
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentjoearunraja2
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6PawanMM
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Arun Gupta
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Arun Gupta
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0Arun Gupta
 
Introduction tomcat7 servlet3
Introduction tomcat7 servlet3Introduction tomcat7 servlet3
Introduction tomcat7 servlet3JavaEE Trainers
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
Managing state in asp.net
Managing state in asp.netManaging state in asp.net
Managing state in asp.netSireesh K
 

Similar a Advance java session 18 (20)

SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11
 
Advance java session 19
Advance java session 19Advance java session 19
Advance java session 19
 
Servlets - filter, listeners, wrapper, internationalization
Servlets -  filter, listeners, wrapper, internationalizationServlets -  filter, listeners, wrapper, internationalization
Servlets - filter, listeners, wrapper, internationalization
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
 
Servlet
ServletServlet
Servlet
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
 
Listeners and filters in servlet
Listeners and filters in servletListeners and filters in servlet
Listeners and filters in servlet
 
Servlet11
Servlet11Servlet11
Servlet11
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Chap4 4 1
Chap4 4 1Chap4 4 1
Chap4 4 1
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0
 
Introduction tomcat7 servlet3
Introduction tomcat7 servlet3Introduction tomcat7 servlet3
Introduction tomcat7 servlet3
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Servlet session 9
Servlet   session 9Servlet   session 9
Servlet session 9
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Managing state in asp.net
Managing state in asp.netManaging state in asp.net
Managing state in asp.net
 

Más de Smita B Kumar

Advance java session 20
Advance java session 20Advance java session 20
Advance java session 20Smita B Kumar
 
Advance java session 17
Advance java session 17Advance java session 17
Advance java session 17Smita B Kumar
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16Smita B Kumar
 
Advance java session 15
Advance java session 15Advance java session 15
Advance java session 15Smita B Kumar
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14Smita B Kumar
 
Advance java session 13
Advance java session 13Advance java session 13
Advance java session 13Smita B Kumar
 
Advance java session 12
Advance java session 12Advance java session 12
Advance java session 12Smita B Kumar
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10Smita B Kumar
 
Advance java session 9
Advance java session 9Advance java session 9
Advance java session 9Smita B Kumar
 
Advance java session 8
Advance java session 8Advance java session 8
Advance java session 8Smita B Kumar
 
Advance java session 7
Advance java session 7Advance java session 7
Advance java session 7Smita B Kumar
 
Advance java session 6
Advance java session 6Advance java session 6
Advance java session 6Smita B Kumar
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5Smita B Kumar
 
Advance java session 4
Advance java session 4Advance java session 4
Advance java session 4Smita B Kumar
 
Advance java session 3
Advance java session 3Advance java session 3
Advance java session 3Smita B Kumar
 
Advance java session 2
Advance java session 2Advance java session 2
Advance java session 2Smita B Kumar
 
01 introduction to struts2
01 introduction to struts201 introduction to struts2
01 introduction to struts2Smita B Kumar
 

Más de Smita B Kumar (18)

Advance java session 20
Advance java session 20Advance java session 20
Advance java session 20
 
Advance java session 17
Advance java session 17Advance java session 17
Advance java session 17
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16
 
Advance java session 15
Advance java session 15Advance java session 15
Advance java session 15
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14
 
Advance java session 13
Advance java session 13Advance java session 13
Advance java session 13
 
Advance java session 12
Advance java session 12Advance java session 12
Advance java session 12
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10
 
Advance java session 9
Advance java session 9Advance java session 9
Advance java session 9
 
Advance java session 8
Advance java session 8Advance java session 8
Advance java session 8
 
Advance java session 7
Advance java session 7Advance java session 7
Advance java session 7
 
Advance java session 6
Advance java session 6Advance java session 6
Advance java session 6
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5
 
Advance java session 4
Advance java session 4Advance java session 4
Advance java session 4
 
Advance java session 3
Advance java session 3Advance java session 3
Advance java session 3
 
Advance java session 2
Advance java session 2Advance java session 2
Advance java session 2
 
JEE session 1
JEE session 1JEE session 1
JEE session 1
 
01 introduction to struts2
01 introduction to struts201 introduction to struts2
01 introduction to struts2
 

Último

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Último (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

Advance java session 18

  • 2. Agenda • Understanding Listener • Monitoring creation and destruction in ServletContext • Recognizing session creation and destruction • Recognizing request creation and destruction
  • 3. Understanding Listeners • JSP is a template page technology - High level abstraction of Servlets • Separation of presentation from logic • Even non java programmer can create JSP pages with reasonable ease
  • 4. Available Listeners • Servlet context listeners. - These listeners are notified when the servlet context (i.e.,the Web application) is initialized and destroyed. • Servlet context attribute listeners. - These listeners are notified when attributes are added to,removed from, or replaced in the servlet context. • Session listeners. - These listeners are notified when session objects are created, invalidated, or timed out. • Session attribute listeners. - These listeners are notified when attributes are added to, removed from, or replaced in any session.
  • 5. Creating a Listeners • Implement the appropriate interface. - Use ServletContextListener, ServletContextAttributeListener, - HttpSessionListener, or HttpSessionAttributeListener. • Override the methods needed to respond to the events of interest. - Provide empty bodies for the other methods in the interface. • Access the important Web application objects. - Six objects that you are likely to use in event-handling methods: • The servlet context • The name of the servlet context attribute that changed • The value of the servlet context attribute that changed • The session object • The name of the session attribute that changed
  • 6. Creating a Listeners • Use these objects. - This process is application specific, but there are some common themes. For example, with the servlet context, you are most likely to read initialization parameters getInitParameter), store data for later access (setAttribute), and read previously stored data (getAttribute). • Declare the listener. - You do this with the listener and listener-class elements of the general Web application deployment descriptor (web.xml) or of a tag library descriptor file. • Provide any needed initialization parameters. - Servlet context listeners commonly read context initialization parameters to use as the basis of data that is made available to all servlets and JSP ages. You use the context-param web.xml element to provide the
  • 7. Monitoring Creation and Destruction • The ServletContextListener class responds to the Initialization and destruction of the servlet context. - These events correspond to the creation and shutdown of the Web application itself. • ServletContextListener is most commonly used to - Set up application-wide resources like database connection pools - Read the initial values of application-wide data that will be used by multiple servlets and JSP pages.
  • 8. Implementing ServletContextListener • Implement the ServletContextListener interface. • Override contextInitialized and contextDestroyed. - contextInitialized is triggered when the Web application is first loaded and the servlet context is created. Most common tasks: • Creating application-wide data (e.g., by reading context init params) • Storing that data in an easily accessible location . - contextDestroyed is triggered when the Web application is being shut down and the servlet context is about to be destroyed. Most common task: • Releasing resources (e.g. closing connections). • Obtain a reference to the servlet context. - The contextInitialized and contextDestroyed methods each take a ServletContextEvent as an argument. - The ServletContextEvent class has a getServletContext method that returns the servlet context
  • 9. Implementing ServletContextListener • Use the servlet context. - Read initialization parameters: getInitParameter - Store data:setAttribute - Make log file entries: log. • Declare the listener. <listener> <listener-class>package.Listener</listener-class> </listener> • Provide needed initialization parameters. <context-param> <param-name>name</param-name> <param-value>value</param-value> </context-param>
  • 10. Implementing ServletContextAttributeListener • Implement ServletContextAttributeListener • Override attributeAdded, attributeReplaced, and attributeRemoved. - attributeAdded is triggered when a new attribute name is first added to the servlet context. - attributeReplaced is triggered when a new value is assigned to an existing name. attributeAdded is not triggered in this case. The old value is obtained via event.getValue and the new value is obtained via context. - getAttribute. attributeRemoved is triggered when a servlet context attribute is removed altogether. • Obtain references to the attribute name, attribute value, and servlet context. - Call the following methods of the event object: getName,getValue, and getServletContext
  • 11. Implementing ServletContextAttributeListener • Use the objects. - You normally compare attribute name to a stored name to see if it is the one you are monitoring. The attribute value is used in an application-specific manner. The servlet context is usually used to read previously stored attributes (getAttribute), store new or changed attributes (setAttribute), and make entries in the log file (log). • Declare the listener. - Use the listener and listener-class elements to list the fully qualified name of the listener class, <listener> <listener-class> somePackage.SomeListener </listener-class> </listener>
  • 12. Recognizing Session Creation and destruction • Implement the HttpSessionListener interface. • Override sessionCreated and sessionDestroyed. - sessionCreated is triggered when a new session is created. - sessionDestroyed is triggered when a a session is destroyed. This destruction could be due to an explicit call to the invalidate method or because the elapsed time since the last client access exceeds the session timeout. - Multithreaded access is possible. Synchronize if necessary. • Obtain a reference to the session and possibly to the servlet context. - Each of the two HttpSessionListener methods takes an HttpSessionEvent as an argument. The HttpSessionEvent class has a getSession method that provides access to the session object.You almost always want this reference; you occasionally also want a reference to the servlet context. If so, first obtain the session object and then call getServletContext on it
  • 13. Recognizing Session Creation and destruction • Use the objects. - One of the only methods you usually call on the session is setAttribute. Do this in sessionCreated if you want to guarantee that all sessions have a certain attribute. - Wait! What about getAttribute? Nope. In sessionCreated, there is nothing in the session yet, so getAttribute is pointless. In addition, all attributes are removed before sessionDestroyed is called, so calling getAttribute is also pointless there. If you want to clean up attributes that are left in sessions that time out, you use the attributeRemoved method of HttpSessionAttributeListener. So, sessionDestroyed is mostly reserved for listeners that are simply keeping track of the number of sessions in use. • Declare the listener. - In web.xml or the TLD file, use listener and listener-class to list fully qualified name of listener class, as below. <listener> <listener-class>package.SomeListener</listener-class> </listener>
  • 14. Using HttpSessionAttributeListener • Implement HttpSessionAttributeListener. • Override attributeAdded, attributeReplaced, and attributeRemoved. - attributeAdded is triggered when a new attribute name is first added to a session. - attributeReplaced is triggered when a new value is assigned to an existing name. attributeAdded is not triggered in this case. The old value is obtained via event.getValue and the new value is obtained via session.getAttribute. - attributeRemoved is triggered when a session attribute is removed altogether. This removal can be due to an explicit programmer call to removeAttribute, but is more commonly due to the system removing all attributes of sessions that are about to be deleted because their timeout expired.
  • 15. Using HttpSessionAttributeListener • Obtain references to the attribute name, attribute value, session, & ServletContext. - The HttpSessionAttributeListener methods take an HttpSessionBindingEvent as args. HttpSessionBindingEvent has three useful methods: getName (name of attribute that was changed), getValue (value of changed attribute—new value for attributeAdded and previous value for attribute Replaced and attributeRemoved), and getSession (the HttpSession object). If you want access to the servlet context, first obtain the session and then call getServletContext on it. • Use the objects. - The attribute name is usually compared to a stored name to see if it is the one you are monitoring. The attribute value is used in an application-specific manner. The session is usually used to read previously stored attributes (getAttribute) or to store new or changed attributes (setAttribute). • Declare the listener. - Use listener and listener-class in web.xml as before. `
  • 16. Summary of Listeners - Servlet context listeners. • Notified when servlet context is initialized and destroyed. - Servlet context attribute Listeners. • Notified when context attributes are added/removed/replaced - Session listeners. • Notified when sessions are created, invalidated, or timed out. - Session attribute listeners. • Notified when session attributes are added/removed/replaced