- Java EE Tutorials
- Servlets Tutorials
- Servlets Interview Questions
While developing web applications (servlets), some data elements are required to process the request at the development phase. For example, in login application we need to enter user name and password. But user name and password are available only at the time of deploying the application. Instead of using database details we can configure the data into servlet by using initialization parameters. Servlet container provides configuration information for servlet in the form of init parameters and context parameters.
Retrieval of Initialization Parameters
The servlet initialization parameters are retrieved by using the ServletConfig object. The following methods provide functionality to retrieve the values of the initialization parameters.
- public String getInitParameter (String): It returns the string value of initialized parameter or it returns null if requested parameter does not exist in the web.xml file.
- public Enumeration getInitParameterNames (): It returns names of all initialization parameters as an Enumeration of string objects or returns empty enumeration if no initialization parameters specified in web.xml file.
- public String getServletName () :It returns name of the servlet.
- public ServletContext getServletContext(): It is method of ServletContext interface which returns information about the servlet.
We can configure init parameters by using the following tag under <servlet> tag after <servlet-class> tag in the web.xml file:
<init-param> <param-name>parameter name</param-name> <param-value>parameter value</param-value> </init-param>
Here, <param-name> tag defines name of the attribute and tag defines string value for attribute.
Example
package javabeat.net.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigDemo extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType ("text/html"); PrintWriter out = response.getWriter (); ServletConfig config= getServletConfig(); String S1 =config. getInitParameter ("r1"); out. print ("r1 value is: " +S1); out.close (); } }
Web.xml:
<web-app> <servlet> <servlet-name>ServletConfigDemo</servlet-name> <servlet-class>javabeat.net.servlets.ServletConfigDemo</servlet-class> <init-param> <param-name>r1</param-name> <param-value>5</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfigDemo</servlet-name> <url-pattern>/ServletConfigDemo</url-pattern> </servlet-mapping> </web-app>
Execute the above class ServletConfigDemo, following output will be displayed.
r1 value is: 5
Context Initialization Parameters
In some cases we want to configure data that has to be retrieved into all or multiple servlets configured in the web.xml file. We can implement them by using the context initialization parameters provided under servlet specification.
We can configure Servlet context parameter by using <context-param> tag in the web.xml file as shown in the following code:
<context-param> <param-value>parameter name</param-name> <param-value>parameter value</param-value> <context-param>
Here, <param-name> tag defines name of the attribute and tag defines string value for attribute.
Retrieval of Context Initialization Parameters
The servlet context initialization parameters are retrieved by using the ServletContext object. The following methods provide functionality to retrieve the values of the context initialization parameters.
- public String getInitParameter (String): It returns the string value of initialized parameter or it returns null if requested parameter does not exist in the web.xml file.
- public Enumeration getInitParameterNames() : It returns names of all context initialization parameters as an Enumeration of string objects or returns empty enumeration if no initialization parameters specified in web.xml file.
- public void setAttribute(String name, Object object) : It is used to set the object in the scope such as request, application or session.
- public object getAttribute(String name) : It is used to return the attribute for the specified string name.
- public void removeAttribute(String name): It is used to remove the attribute name from the servlet context.
Example
package javabeat.net.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.println(getServletContext().getInitParameter("Welcome")); } }
Web.xml file
<web-app> <servlet> <servlet-name>ServletContextDemo</servlet-name> <servlet-class>javabeat.net.servlets.ServletContextDemo</servlet-class> </servlet> <context-param> <param-name>Welcome</param-name> <param-value>hello world!!!!!!!!!</param-value> </context-param> <servlet-mapping> <servlet-name> ServletContextDemo </servlet-name> <url-pattern>/ ServletContextDemo </url-pattern> </servlet-mapping> </ web-app>
Execute the above class ServletContextDemo, following output will be displayed.
hello world!!!!!!!!!
Previous Tutorial : ServletInputStream and ServletOutputStream || Next Tutorial : How To Set PDF File Display In Servlet