Skip to main content

JSP Config implicit Object

  • ServletConfig available in javax.servlet.*; package
  • ServletConfig object is one per servlet class.
  • ServletConfig object is used to pass information to specific servlet or JSP.
  • Object of ServletConfig will be created during initialization process of the servlet.

Let’s Understand it With Example

Project Structure

web.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<display-name>JSPImplicitObject</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
	<servlet>
 
	<servlet-name>JSP</servlet-name>
	<jsp-file>/ConfigObject.jsp</jsp-file>
 
	<init-param>
	   <param-name>name</param-name>
	   <param-value>Bhavesh</param-value>
	</init-param>
 
	</servlet>
 
	<servlet-mapping>
	   <servlet-name>JSP</servlet-name>
	   <url-pattern>/JSP</url-pattern>
	</servlet-mapping>
</web-app>

ConfigObject.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
<%@ page language="java" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
 
<%
 
String name=config.getInitParameter("name");
 
out.println(" Hello "+name);
 
%>
 
</body>
</html>

output: