Skip to content
Related Articles

Related Articles

Improve Article

JSP Config – Implicit Objects

  • Last Updated : 28 Jul, 2021
Geek Week

JSP Config is an implicit object which is used to transmit the configuration details to the JSP page. In JSP, Config is an instance of type ServletConfig. This implicit object is used to acquire an initialization parameter for a certain JSP page. For each JSP page, the config object is generated through the web container. JSP’s config object carries the configuration pieces of information like the username, password, driver name, servlet name, servlet context, specification names, and their values settle in the web.xml (configuration file). 

It is an object of type javax. servlet.ServletConfig interface.

Through web.xml file the detail is send to JSP file. To fetch this detail the Config object is used. Normally, it is used extensively for the initialization parameters like as the paths or file locations from the web.xml file.

Note: JSP Config object has scope only up to a single JSP page.

Methods of ServletConfig interface are listed alongside the action performed from the below table as follows:



Methods

Explanation

ServletContext getServletContext()          From the session, this method withdraws an object with a name.
String getInitParameter(String name)This method receives an object saved in session with a name, or null.
Enumeration getInitParameterNames()In a session, this method places an object with a name.
String getServletName()This method gives back a RequestDispatcher that behaves as a cover for the resource at the path.

Implementation: 

We will be proposing examples directly and later on will append the visual aid representing JSP pages as an output. So example of JSP Config implicit object is as follows:

  • In this example, the various variables and their values are contained in the ‘web.xml file‘. 
  • The config object is in ‘index.jsp‘ file retrieves that information and displays it to the user. 

Example 1: index.html File 

HTML




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
  
</body>
</html>

Example 2: web.xml File

XML




<?xml version="1.0" encoding="UTF-8"?>
         id="WebApp_ID" version="4.0">
  <display-name>HelloWorld</display-name>
  <servlet>
  <servlet-name>welcome</servlet-name>
  <jsp-file>/welcome.jsp</jsp-file>
    
  <init-param>
  <param-name>dname</param-name>
  <param-value>com.mysql.jdbc.Driver</param-value>
  </init-param>
    
  </servlet>
    
  <servlet-mapping>
  <servlet-name>welcome</servlet-name>
  <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

Example 3: ‘welcome.jsp’ FIle 

HTML




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>GeeksforGeeks</title>
</head>
<body>
  
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("<br/>driver name is="+driver);  
%>
  
</body>
</html>

Output:

These are the screenshots of the outputs of the above JSP page.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :