In this tutorial, you will learn config implicit object in JSP. It is an instance of javax.servlet.ServletConfig. The JSP config implicit object is used for getting configuration information for a particular JSP page. Using application implicit object we can get application-wide initialization parameters, however using Config we can get initialization parameters of an individual servlet mapping.
Methods of Config Implicit Object
- String getInitParameter(String name): It returns the value of Initialization parameter for a given parameter name.
<web-app> … <context-param> <param-name>parameter1</param-name> <param-value>ValueOfParameter1</param-value> </context-param> </web-app>
This is the web.xml file
String s=application.getInitParameter(“parameter1”);
The value of s will be “ValueOfParameter1”. Still confused where did it come from? See the param-value tag in he above web.xml file.
- Enumeration getInitParameterNames(): Returns enumeration of Initialization parameters.
- ServletContext getServletContext(): This method returns a reference to the Servlet context.
- String getServletName(): It returns the name of the servlet which we define in the web.xml file inside
<servlet-name>
tag.
JSP config implicit object Example
Following is the web.xml file. In this file, servlet name and mappings are defined. We can fetch these details using config implicit object. In this example, we are demonstrating how to fetch these details using config implicit object.
web.xml
<web-app> <servlet> <servlet-name>BeginnersBookServlet</servlet-name> <jsp-file>/index.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>BeginnersBookServlet</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> </web-app>
index.jsp
In this JSP page we are calling getServletName()
method of config object for fetching the servlet name from web.xml file.
<html> <head> <title> Config Implicit Object</title> </head> <body> <% String sname=config.getServletName(); out.print("Servlet Name is: "+sname); %> </body> </html>
Output: This is the output screen for the above JSP page. In the web.xml file, we have defined the servlet name as “BeginnersBookServlet”. In the index.jsp page, we are fetching the servlet name using config implicit object. The fetched value then displayed on the screen using out.print()
method.
Medha says
Hi, Let’s say we have multiple servlet mappings in web.xml(as it is usually the case), how would you fetch that particular servlet name from the group of mappings in the web.xml?