Servlet Part-3
ServletConfig Intreface, ServletContext, Attribute In Servlets….
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.
Each Servlet a ServletConfig object is created and passed to the init (ServletConfig config) to initialize the parameters . The config object gets values from 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.
Example:
Create a dynamic web project and web.xml file and servlet class.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>controlServlet</servlet-name>
<servlet-class>test.ControlServlet</servlet-class>
<!-- this element is used to declare the parameters -->
<init-param>
<!-- this element is used to declare paramters name where we use this
name in our prgm to get value-->
<param-name>count</param-name>
<!-- this element is used to declare the parameters value -->
<param-value>10</param-value>
</init-param>
<!-- this is second parameter-->
<init-param>
<param-name>rate</param-name>
<param-value>20</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>controlServlet</servlet-name>
<url-pattern>/pqr</url-pattern>
</servlet-mapping>
</web-app> ControlServlet
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.catalina.startup.ContextConfig;
public class ControlServlet extends GenericServlet
{
int count;
public void init(ServletConfig config)
{
count= Integer.parseInt(config.getInitParameter("count"));
System.out.println(count);
}
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
pw.print("This value from Servlet config"+"=="+count);
}
}Output
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.
Get Spring Techie’s stories in your inbox
Join Medium for free to get updates from this writer.
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.
Advantages
- 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:
- public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
- 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 void removeAttribute(String name):Removes the attribute with the given name from the servlet context.
To get the object of Servlet Context interface we have two methods.
- getServletContext() method of Servlet Config interface returns the object of ServletContext.
- getServletContext() method of Generic Servlet class returns the object of ServletContext.
Example: Create a dynamic web project and web.xml file and servlet class.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<web-app>
<!-- this element used to declare servletcontext
this has scope at application level
it is only one ServletContext object per web application -->
<context-param>
<param-name>contextvar</param-name>
<param-value>1200</param-value>
</context-param>
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>test.First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/pqr</url-pattern>
</servlet-mapping>
</web-app>First.java
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class First extends HttpServlet
{
private static final long serialVersionUID = 1L;
public ServletContext sct=null;
public void init(ServletConfig config)
{
//returns the object of ServletContext.
sct=config.getServletContext();
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
int contextvar=Integer.parseInt(sct.getInitParameter("contextvar"));
pw.println("This is form Servlet context:=="+contextvar);
}
}Output
Difference between ServletContext and ServletConfig.
- ServletContext is created only one for entire web application ,whereas ServletConfig object is created one for each servlet class.
- The servletconfig object refers to the single servlet whereas servletcontext object refers to the whole web application.
Attribute in Servlet
Defination: An Attribute is another term for a field , it is typically a public constant or public variable that can accessed in any directly.
“The servlet programmer can pass informations from one servlet to another using attributes. It is just like passing object from one class to another so that we can reuse the same object again and again.”
Attribute specific methods of ServletRequest, HttpSession and ServletContext interface.
- 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.
Example: Create a dynamic web project and web.xml file and two servlet classes.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<web-app>
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>test.First</servlet-class>
</servlet>
<servlet>
<servlet-name>second</servlet-name>
<servlet-class>test.Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/pqr</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>second</servlet-name>
<url-pattern>/xyz</url-pattern>
</servlet-mapping>
</web-app>First.java
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class First extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
ServletContext sc=req.getServletContext();
// setting attribute values
sc.setAttribute("cval", 1200);
req.setAttribute("rval", 1100);
RequestDispatcher dispatcher = req.getRequestDispatcher("/xyz");
dispatcher.forward(req, res);
}
}Second.java
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Second extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException
{
PrintWriter pw = res.getWriter();
res.setContentType("text/hmtl");
ServletContext sc=req.getServletContext();
// getting attributes from servlet 1
Integer cval=(Integer)sc.getAttribute("cval");
Integer rval =(Integer)req.getAttribute("rval");
// removing attributes
//req.removeAttribute("rval");
// sc.removeAttribute("cval");
pw.println("cavl"+cval);
pw.println("rval"+rval);
}
}Copy Paste this codes in your eclipse IDE , practise yourself…






