ServletConfig Interface & ServletContext
Interface
ServletConfig
• An object of ServletConfig is created by the
web container for each servlet. This object can
be used to get configuration information from
[Link] file.
• If the configuration information is modified
from the [Link] file, we don't need to
change the servlet. So it is easier to manage
the web application if any specific content is
modified from time to time.
Advantage of ServletConfig
The core advantage of ServletConfig is that you
don't need to edit the servlet file if
information is modified from the [Link] file.
Methods of ServletConfig interface
• public String getInitParameter(String name):Returns
the parameter value for the specified parameter
name.
• public Enumeration
getInitParameterNames():Returns an enumeration of
all the initialization parameter names.
• public String getServletName():Returns the name of
the servlet.
• public ServletContext getServletContext():Returns an
object of ServletContext.
How to get the object of ServletConfig
• getServletConfig() method of Servlet interface
returns the object of ServletConfig.
• Syntax of getServletConfig() method
• public ServletConfig getServletConfig();
• Example of getServletConfig() method
• ServletConfig config=getServletConfig();
Syntax to provide the initialization parameter for a servlet
• The init-param sub-element of servlet is used to specify the initialization
parameter for a servlet.
• <web-app>
• <servlet>
• ......
•
• <init-param>
• <param-name>parametername</param-name>
• <param-value>parametervalue</param-value>
• </init-param>
• ......
• </servlet>
• </web-app>
Servlet Code to Access parameter
import [Link].*;
import [Link].*;
import [Link].*;
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
ServletConfig config=getServletConfig();
String driver=[Link]("driver");
[Link]("Driver is: "+driver);
[Link]();
}
}
ServletContext Interface
An object of ServletContext is created by the
web container at time of deploying the project.
This object can be used to get configuration
information from [Link] file. There is only
one ServletContext object per web application.
If any information is shared to many servlet, it is
better to provide it from the [Link] file using
the <context-param> element.
Advantage of ServletContext
Easy to maintain if any information is shared to
all the servlet, it is better to make it available
for all the servlet.
We provide this information from the [Link]
file, so if the information is changed, we don't
need to modify the servlet. Thus it removes
maintenance problem.
Usage of ServletContext Interface
• There can be a lot of usage of ServletContext object.
Some of them are as follows:
• The object of ServletContext provides an interface
between the container and servlet.
• The ServletContext object can be used to get
configuration information from the [Link] file.
• The ServletContext object can be used to set, get or
remove attribute from the [Link] file.
• The ServletContext object can be used to provide inter-
application communication.
Commonly used methods of ServletContext
interface
• There is given some commonly used methods of ServletContext interface.
public String getInitParameter(String name):Returns the parameter value
for the specified parameter name.
• public Enumeration getInitParameterNames():Returns the names of the
context's initialization parameters.
• public void setAttribute(String name,Object object):sets the given object
in the application scope.
• public Object getAttribute(String name):Returns the attribute for the
specified name.
• public Enumeration getInitParameterNames():Returns the names of the
context's initialization parameters as an Enumeration of String objects.
• public void removeAttribute(String name):Removes the attribute with
the given name from the servlet context.
How to get the object of ServletContext
interface
• getServletContext() method of ServletConfig
interface returns the object of ServletContext.
• getServletContext() method of GenericServlet
class returns the object of ServletContext.
Example of getServletContext() method
//We can get the ServletContext object from ServletConfig object
ServletContext application=getServletConfig().getServletContext();
//Another convenient way to get the ServletContext object
ServletContext application=getServletContext();
Syntax to provide the initialization
parameter in Context scope
• The context-param element, subelement of web-app, is used to define the
initialization parameter in the application scope. The param-name and
param-value are the sub-elements of the context-param. The param-name
element defines parameter name and and param-value defines its value.
• <web-app>
• ......
•
• <context-param>
• <param-name>parametername</param-name>
• <param-value>parametervalue</param-value>
• </context-param>
• ......
• </web-app>
Example
• <web-app>
•
• <servlet>
• <servlet-name>sonoojaiswal</servlet-name>
• <servlet-class>DemoServlet</servlet-class>
• </servlet>
•
• <context-param>
• <param-name>dname</param-name>
• <param-value>[Link]</param-value>
• </context-param>
•
• <context-param>
• <param-name>username</param-name>
• <param-value>system</param-value>
• </context-param>
•
• <context-param>
• <param-name>password</param-name>
• <param-value>oracle</param-value>
• </context-param>
•
• <servlet-mapping>
• <servlet-name>sonoojaiswal</servlet-name>
• <url-pattern>/context</url-pattern>
• </servlet-mapping>
•
• </web-app>
Example 1 setAttribute
• public class DemoServlet1 extends HttpServlet{
• public void doGet(HttpServletRequest req,HttpServletResponse res)
• {
• try{
•
• [Link]("text/html");
• PrintWriter out=[Link]();
•
• ServletContext context=getServletContext();
• [Link]("company","IBM");
•
• [Link]("Welcome to first servlet");
• [Link]("<a href='servlet2'>visit</a>");
• [Link]();
•
• }catch(Exception e){[Link](e);}
•
• }}
Example 2 (getAttribute call)
• public class DemoServlet2 extends HttpServlet{
• public void doGet(HttpServletRequest req,HttpServletResponse res)
• {
• try{
•
• [Link]("text/html");
• PrintWriter out=[Link]();
•
• ServletContext context=getServletContext();
• String n=(String)[Link]("company");
•
• [Link]("Welcome to "+n);
• [Link]();
•
• }catch(Exception e){[Link](e);}
• }}