0% found this document useful (0 votes)
73 views

ServletConfig Interface

The document discusses the ServletConfig and ServletContext interfaces in Java servlets. ServletConfig provides configuration information for each servlet instance, while ServletContext provides configuration shared across all servlets in an application. Both interfaces allow accessing initialization parameters from web.xml without modifying servlet code. ServletContext also enables attribute sharing between servlets.

Uploaded by

Akshat Upneja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

ServletConfig Interface

The document discusses the ServletConfig and ServletContext interfaces in Java servlets. ServletConfig provides configuration information for each servlet instance, while ServletContext provides configuration shared across all servlets in an application. Both interfaces allow accessing initialization parameters from web.xml without modifying servlet code. ServletContext also enables attribute sharing between servlets.

Uploaded by

Akshat Upneja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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
web.xml file.
• If the configuration information is modified
from the web.xml 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 web.xml 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 java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class DemoServlet extends HttpServlet {  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
      
    ServletConfig config=getServletConfig();  
    String driver=config.getInitParameter("driver");  
    out.print("Driver is: "+driver);  
          
    out.close();  
    }  
  
}  
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 web.xml 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 web.xml 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 web.xml


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 web.xml file.
• The ServletContext object can be used to set, get or
remove attribute from the web.xml 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>sun.jdbc.odbc.JdbcOdbcDriver</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{  
•   
• res.setContentType("text/html");  
• PrintWriter out=res.getWriter();  
•   
• ServletContext context=getServletContext();  
• context.setAttribute("company","IBM");  
•   
• out.println("Welcome to first servlet");  
• out.println("<a href='servlet2'>visit</a>");  
• out.close();  
•   
• }catch(Exception e){out.println(e);}  
•   
• }}  
Example 2 (getAttribute call)
• public class DemoServlet2 extends HttpServlet{  
• public void doGet(HttpServletRequest req,HttpServletResponse res)  
• {  
• try{  
•   
• res.setContentType("text/html");  
• PrintWriter out=res.getWriter();  
•   
• ServletContext context=getServletContext();  
• String n=(String)context.getAttribute("company");  
•   
• out.println("Welcome to "+n);  
• out.close();  
•   
• }catch(Exception e){out.println(e);}  
• }}  

You might also like