Advertisement



< Prev
Next >



JSP config object





JSP's config object holds the configuration details like the username, password, parameter names and their values set in the configuration file(web.xml) and it is an object of type javax.servlet.ServletConfig interface.




Methods of ServletConfig interface


Methods Description
String getInitParameter(String name) This method gets an object stored in a session with a name, or null.
Enumeration getInitParameterNames() This method sets an object with a name in a session.
ServletContext getServletContext() This method removes an object with a name from the session.
String getServletName() This method returns a RequestDispatcher which acts as a wrapper for the resource at the path.



Advertisement




An example of using the config object


In the upcoming example, we are first going to set the configuration data by setting a few variables and their values in the configuration file, web.xml as shown below.



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome tomcat</display-name>
  <description>
     Welcome tomcat
  </description>

<servlet>
 	<servlet-name>MyJspPage</servlet-name>
	<jsp-file>/JspConfig.jsp</jsp-file>
	<init-param>
		<param-name>Name</param-name>
		<param-value>DecodeJava</param-value>
	</init-param>	

	<init-param>
		<param-name>Year</param-name>
		<param-value>2018</param-value>
	</init-param>	

</servlet>

<servlet-mapping>
	<servlet-name>MyJspPage</servlet-name>
	<url-pattern>/JspConfig.jsp</url-pattern>
</servlet-mapping>

</web-app>



and next we are going to retrieve the values of these configured variables using the JSP inbuilt config object and display it to the user.


JspConfig.jsp
<html>
<head>	
<title>Using JSP config object</title>
</head>

<body>

<% 
String sname = config.getServletName();
out.println("Servlet Name is "+ sname); 
%>

<br/>

<% 
out.println("Name variable retreived using config object : " + config.getInitParameter("Name")); 
out.println("<br/>");
out.println("Count variable retreived using config object : " + config.getInitParameter("Count"));
%>

</body>
</html>




Executing the JspConfig.jsp gives us the following window displaying the values of initialized variables/parameters accessed using config JSP implicit object.



JspConfig.jsp





Please share this article -




< Prev
Next >
< JSP application object
JSP exception object >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement