Step7: ServletConfig and ServletContext + code

Learn with our youtube video –

ServletConfigServletContext
Represents the configuration for a single servlet, specified in the web deployment descriptor.Represents the configuration for all servlets in a web application, also specified in the deployment descriptor.
Can be used to retrieve initialization parameters specific to a servlet.Can be used to retrieve initialization parameters for the entire web application, as well as attributes that are shared across all servlets.
Is unique to each servlet.Is shared among all servlets in a web application.
Is passed to the init() method of a servlet.Can be accessed by any servlet using the getServletContext() method of the ServletConfig interface.
tfbjt

ServletConfig interface-

Definition- Servlet config is used to pass information to a servlet during initialization.

Step 1- Initialization of key-value pair in web.xml for particular servlet

Step 2-Access value using key in servlet.

Example 5- Web app to create a servlet which gets data from web.xml using ServletConfig interface.

Project 3 files-

1-Zx.java(inside package=’ab’)

2-index.html(inside WebContent folder)

3-web.xml (inside WEB-INF folder)

1-Zx.java(servlet)-

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletConfig sc=getServletConfig();
String a=sc.getInitParameter("myName");

PrintWriter out=response.getWriter();
out.print(a);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Tempweb</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>q</servlet-name>
<servlet-class>ab.Zx</servlet-class>
<init-param>
<param-name>myName</param-name>
<param-value>Mr.java</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>q</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>

3-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">Click here</a>
</body>
</html>

Note – For annotation version –

@WebServlet(
name = "Zx", urlPatterns = {"/first"}
,initParams = {
@WebInitParam(name= "myName :", value="Mr. Java")
}

ServletContext Interface- 

Definition-Used to set/get configuration/object information at global level/application level.

-Available for all servlets or JSPs of the Web app.

There are two ways to set configuration for servlet context –

1- Static Configuration : Using <context-param> in web.xml

2- Dynamic Configuration : Using setInitParameter on ServletContext object

1- Static Configuration : Using <context-param> in web.xml

Step 1- Initialization of key-value pair in web.xml for all servlets

Step 2-Access value using key in any servlet

Example 6- Web app to create any number of servlets which get data from web.xml using ServletContext interface.

Project 4 files-

1-Yz.java(inside package= ‘ab’)

2-Zx.java(inside package=’ab’)

3-web.xml(inside WEB-INF)

4-index.html(inside WebContent)

1-Yz.java(Servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Yz extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-Zx.java(servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}


3-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Tempweb</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>

<context-param>
<param-name>myName</param-name>
<param-value>Mr.java</param-value>

</context-param>

<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>

</web-app>


4-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">Click here servlet1</a>
<a href="second">Click here servlet2</a>
</body>
</html>

2- Dynamic Configuration: Using setInitParameter on ServletContext object

Example 7- Web app to create any number of servlets which set/get data from setInitParameter /getInitParameter using ServletContext interface.

Project 4 files-

1-Yz.java(inside package= ‘ab’)

2-Zx.java(inside package=’ab’)

3- MyServletContextListener (Implementing ServletContextListener interface)

3-web.xml(inside WEB-INF)

4-index.html(inside WebContent)

1-Yz.java(Servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Yz extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-Zx.java(servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
String a= sc.getInitParameter("MyName");

PrintWriter out=response.getWriter();
out.print(a);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3- MyServletContextListener.java

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.setInitParameter("MyName", "kuldeep");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // Cleanup code if needed
    }
}

4-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Tempweb</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>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>

</web-app>

5-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">Click here servlet1</a>
<a href="second">Click here servlet2</a>
</body>
</html>

Apart from these- We can set/get object at application level using setAttribute/getAttribute methods

Example 8- Web app to create any number of servlets which set/get object data from setAttribute/getAttribute using ServletContext interface.

Project 4 files-

1-Yz.java(inside package= ‘ab’)

2-Zx.java(inside package=’ab’)

3-web.xml(inside WEB-INF)

4-index.html(inside WebContent)

1-Yz.java(Servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Yz extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
HashMap maps = new HashMap();
maps.put("a","abc");
sc.setAttribute("MyMap",maps );
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

2-Zx.java(servlet)

package ab;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Zx extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = getServletContext();
HashMap mp = sc.getAttribute("MyMap");
String mpValue = mp.get("a");
System.out.println(mpValue);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);
}

}

3-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Tempweb</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>a</servlet-name>
<servlet-class>ab.Yz</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.Zx</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>

</web-app>

4-index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="first">Click here servlet1</a>
<a href="second">Click here servlet2</a>
</body>
</html>