implicit object config JSP Example

implicit object config JSP Example

Note: It is advised to read about ServletConfig interface in Servlets where at length discussed its importance, methods. One example is also given using ServletConfig in Servlet web xml init param Example using ServletConfig.

config is one of the 9 implicit objects, JSP supports. config is an object of javax.servlet.ServletConfig interface. This interface is used to read the configuration particulars of a Servlet/JSP. I use the term "Servlets" in "JSP" very often as there is a very narrow line between them as a JSP is internally converted into a Servlet and executed.

Programmer uses config object to read the <init-param> tag of web.xml file. In <init-param> tag, Programmer can write some data that can be read by a Servlet/JSP. The <init-param> data is specific to a Servlet/JSP. That is <init-param> data of one Servlet/JSP cannot be read by other Servlet/JSP.

We have seen in the advantages of JSP over Servlets, writing web.xml for alias name is not mandatory in JSP. And so far, in all the basic examples, we have avoided to write web.xml for JSP. But now it becomes necessary to write the web.xml file for JSP file with an alias name to write <init-param> data.

Example on implicit object config JSP

Let us think, the name of the JSP file is InitData.jsp.

web.xml entry for InitData.jsp

In the web.xml, the modification is to replace <servlet-class>something </servlet-class> with <jsp-file>InitData.jsp</jsp-file>.

There are three <init-param> tags which will be read and printed using JSP file.

File Name: InitData.jsp


Let us see the code.
getServletName() method of config object returns the <servlet-name> of JSP file written in web.xml file; InitData1.

Enumeration e = config.getInitParameterNames();

getInitParametersNames() method of config object returns an object of java.util.Enumeration interface. The Enumeration object e contains all <param-name> data like Institute and Faculty etc. but not their associated values.

String name = (String) e.nextElement();
String value = config.getInitParameter(name);

The nextElement() method of Enumeration interface returns an object of Object class and is type casted to String. The string "name" contains the names like Institute and Faculty etc. The getInitParameter(name) returns the value of <param-value> tag associated with <param-name> like S N Rao and J2EE etc.

What is the config implicit object?

The config object is an implicit object of type javax.servlet.ServletConfig. The ServletConfig has a page scope, that is, the whole JSP file can make use of this object.

The config can be used to retrieve the configuration parameters that are specific to a JSP page. These parameters are set up within the deployment descriptor, web.xml.

The config object can be used to retrieve the ServletContext object through its getServletContext() method. The ServletCotext object is JSP is represented by implicit object "application". "application" is one of the 9 implicit objects, JSP supports.

Leave a Reply

Your email address will not be published. Required fields are marked *