JavaServer Pages (JSP) provides implicit objects to simplify web development. The config implicit object is used to access configuration details of a JSP page. It helps retrieve initialization parameters defined in the web.xml file.
- The config object is an instance of ServletConfig.
- It is used to fetch init parameters from web.xml.
- Its scope is limited to a single JSP page.
Methods of ServletConfig
Methods of ServletConfig interface are listed alongside the action performed from the below table as follows:
- getServletContext(): Returns the ServletContext object
- getInitParameter(String name): Returns the value of a specific initialization parameter
- getInitParameterNames(): Returns all parameter names
- getServletName(): Returns the name of the servlet/JSP page
Step-by-Step Implementation
Step 1: Create index.html
- Create a simple form to take user input
- Use action="welcome" to map request to JSP
index.html File
<!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>
Step 2: Configure web.xml
- Define servlet name and JSP file mapping
- Add <init-param> to store configuration values
- Map URL pattern to access JSP
web.xml File
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html"
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html/web-app_4_0.xsd"
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>
Step 3: Create welcome.jsp
- Access user input using request.getParameter()
- Use config.getInitParameter() to fetch value
- Display output using out object.
welcome.jsp FIle
<%@ 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>
Step 4: Run the Application
- Deploy project on server (e.g., Tomcat)
- Open browser and run index.html
- Enter name and click go
Output:
These are the screenshots of the outputs of the above JSP page.
Advantages
- Helps separate configuration from code, improving maintainability.
- Allows easy access to initialization parameters defined in web.xml.
- Provides page-specific configuration, making it flexible for different JSP pages.