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.

Output-cnf

Project Structure-

cnfg2

3 files-

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

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

3-index.html(inside WebContent 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>Exconfig</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>

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.

Output-asd31

Project Structure-

xcn

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>ExCntxt</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>
Advertisements