ServletConfig and ServletContext Object

ServletConfig object


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

Scope of ServletConfig Object:
As long as a servlet is executing, ServletConfig object will be available,
it will be destroyed once the servlet execution is completed.

we write <init-param> under attribute of web.xml

i.e

1
2
3
4
5
6
7
8
9
10
11
         <servlet>
 
		<servlet-name>ConfigObject</servlet-name>
		<servlet-class>com.spy.javaforlearn.ConfigObject</servlet-class>
 
		<init-param>
		    <param-name>myblog</param-name>
		    <param-value>www.javaforlearn.com</param-value>
		</init-param>
 
	</servlet>

Let’s understand it with Example:


Create Dynamic Web Project in Eclipse


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
 
	<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>ConfigObject</servlet-name>
		<servlet-class>com.spy.javaforlearn.ConfigObject</servlet-class>
 
		<init-param>
		<param-name>season1</param-name>
		<param-value>Prison Break</param-value>
		</init-param>
 
		<init-param>
		<param-name>myblog</param-name>
		<param-value>www.javaforlearn.com</param-value>
		</init-param>
 
	</servlet>
 
 
	<servlet-mapping>
		<servlet-name>ConfigObject</servlet-name>
		<url-pattern>/ConfigObject</url-pattern>
	</servlet-mapping>
 
 
	<context-param>
	     <param-name>First_name</param-name>
	     <param-value>Bhavesh</param-value>
	</context-param>
 
	<context-param>
	    <param-name>Last_name</param-name>
	    <param-value>Lakhani</param-value>
	</context-param>
 
 
	<servlet>
		<servlet-name>Servlet1</servlet-name>
		<servlet-class>com.spy.javaforlearn.Servlet1</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>Servlet1</servlet-name>
		<url-pattern>/Servlet1</url-pattern>
	</servlet-mapping>
 
	<servlet>
		<servlet-name>Servlet2</servlet-name>
		<servlet-class>com.spy.javaforlearn.Servlet2</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>Servlet2</servlet-name>
		<url-pattern>/Servlet2</url-pattern>
	</servlet-mapping>
 
</web-app>

ConfigObject.java

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
31
32
33
34
35
36
37
package com.spy.javaforlearn;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class ConfigObject extends HttpServlet 
{
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
	{
 
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		//getting config Object
 
		ServletConfig config=getServletConfig();
 
		String season1= config.getInitParameter("season1");
 
		out.print("<h1>My favourite TV show is:</h1> "+season1);
 
                String myblog= config.getInitParameter("myblog");
 
		out.print("<h1>This is My Blog :</h1> "+myblog);
 
	}
 
}

output:

Accessing ConfigObject servlet

ServletContext Object


  • ServletContext available in javax.servlet.*; package
  • ServletContext is an object of HttpServletContext and It is used pass information to the any Servlet which is present in Application.
  • ServletContext object created by container when server started.
  • ServletContext object is global to entire web application

Scope of ServletContext:
As long as web application is executing, ServletContext object will be available, and it will be destroyed once the application is removed from the server.
In web.xml – tag will be appear under tag

syntax:

1
2
3
4
5
6
7
8
9
 
<web-app>
 
	<context-param>
	<param-name>First_name</param-name>
	<param-value>Bhavesh</param-value>
	</context-param>
 
</web-app>

Passing Context param value in Servlet1

Servlet1.java

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
31
32
33
34
35
36
37
package com.spy.javaforlearn;
 
import java.io.IOException;
import java.io.PrintWriter;
 
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class Servlet1 extends HttpServlet {
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		out.println("This is Servlet1 <br> ");
 
		//getting context Object
 
		ServletContext context=getServletContext();
 
		String f_name= context.getInitParameter("First_name");
 
		out.println("first_name: "+f_name+"<br>");
 
                String l_name= context.getInitParameter("Last_name");
 
		out.println("Last_name: "+l_name+"<br>");
 
	}
}

output:

Passing Context param value in Servlet2

Servlet2.java

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
31
32
33
34
35
36
37
package com.spy.javaforlearn;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class Servlet2 extends HttpServlet {
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		out.println("This is Servlet2 <br>");
 
		//getting context Object
 
		ServletContext context=getServletContext();
 
		String f_name= context.getInitParameter("First_name");
 
		out.print("first_name: "+f_name+"<br>");
 
                String l_name= context.getInitParameter("Last_name");
 
		out.print("Last_name: "+l_name);
 
	}
}

output:

Leave a Reply

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