0% found this document useful (0 votes)
49 views1 page

ServletConfig, ServletContext, ServletContextListener

ServletConfig and ServletContext allow values like email addresses to be configured in web.xml rather than hardcoded in servlets, avoiding recompilation when values change. ServletConfig values are accessible only to a single servlet, while ServletContext values are accessible application-wide. ServletContextListener notifies when the web application starts or stops, allowing objects to be shared across the application.

Uploaded by

Richmond Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views1 page

ServletConfig, ServletContext, ServletContextListener

ServletConfig and ServletContext allow values like email addresses to be configured in web.xml rather than hardcoded in servlets, avoiding recompilation when values change. ServletConfig values are accessible only to a single servlet, while ServletContext values are accessible application-wide. ServletContextListener notifies when the web application starts or stops, allowing objects to be shared across the application.

Uploaded by

Richmond Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Notes on ServletConfig, Servlet Context, ServletContextListener

ServletConfig and ServletContext

- Reasons why you use ServletConfig and ServletContext


Let’s say you have a value like an email, hardcoding it in your servlet for
you to build and run is bad practice. What if the user changes his email. You
would have to change the email again and recompile and run the whole code.
Due to this problem ServletConfig and ServletContext is used. You only need
to edit inside the web.xml, no need to modify and recompile anything in the
servlet.

- ServletConfig
o Values are only accessible to one servlet per servletConfig
o Can have one servletConfig per Servlet
o Let’s say you have 10 servlets then you could have 10
servletConfigs
o <init-param>
o Can only handle values that are Strings
o Retrieving data with the use of getInitParameter

- ServletContext
o Values are accessible to the whole web application including JSP’s
and Servlets
o It is like a static variable in java
o Used usually when creating Header and Footer
o <context-param>
o Can only handle values that are Strings
o Retrieving data with the use of getInitParameter

ServletContextListener
- Is called when the web application is first created
- Needs to be implemented in order to be used
- A class or model is needed for it to be used, the class contains 2
methods: contextInitialized and contextDestroyed
- <listener>
- Handle values like objects that you want to share to the whole web
application
- -Servlets then are used as a controller to pass the object value around
the web application
- ServletContextEvent is used as a notification of ServletContextListener
when values are pass of ServletConfig and ServletContext

You might also like