Step7: ServletConfig and ServletContext + code

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 get configuration information from Deployment Descriptor(web.xml).

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

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>