Вы находитесь на странице: 1из 8

ServletConfig Interface

An object of ServletConfig is created by the web container for each servlet. This
object can be used to get configuration information from web.xml file.
If the configuration information is modified from the web.xml file, we don't need to
change the servlet.
Methods of ServletConfig interface
1. public String getInitParameter(String name):Returns the parameter
value for the specified parameter name.
2. public Enumeration getInitParameterNames():Returns an enumeration
of all the initialization parameter names.
3. public String getServletName():Returns the name of the servlet.
4. public ServletContext getServletContext():Returns an object of
ServletContext.

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletGetAndPost</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>Manager</servlet-name>
<servlet-class>com.rks.servlet.Manager</servlet-class>
<init-param>
<param-name>year</param-name>
<param-value>2015</param-value>
</init-param>
<init-param>
<param-name>County</param-name>
<param-value>India</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Manager</servlet-name>
<url-pattern>/Manager</url-pattern>
</servlet-mapping>
</web-app>

package com.rks.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import
import
import
import
import

javax.servlet.ServletConfig;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class Manager extends HttpServlet {


protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
out.println("Inside doPost Manager");
/* Creating ServletConfig object */
ServletConfig config = getServletConfig();
/* Method 1: config.getInitParameter(String name);
* Return String */
String year = config.getInitParameter("year");
out.print("Year is : " +year);
/* Method 2: config.getInitParameterNames();
* Return Enumeration */
Enumeration<Object> e = config.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
out.print("<br>name : " + name);
String value = config.getInitParameter(name);
out.print("Value : " + value + "<br>");
}
/*
* Method 3: config.getServletName();
* Return String
* It will return name of Servlet
*/
String servletName = config.getServletName();
out.print("<br> Servleet Name is : " + servletName);
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println("<br>Username = " + username);
out.println("<br>Password = " + password);
out.close();
}

ServletContext

An object of ServletContext is created by the web container at time of deploying the project. This object can
be used to get configuration information from web.xml file. There is only one ServletContext object per web
application.
If any information is shared to many servlet, it is better to provide it from the web.xml file using
the <context-param> element.
There is given some commonly used methods of ServletContext interface.
1. public String getInitParameter(String name):Returns the parameter value for the specified
parameter name.
2.

public Enumeration getInitParameterNames():Returns the names of the context's initialization


parameters.

3.

public void setAttribute(String name,Object object):sets the given object in the application
scope.

4.

public Object getAttribute(String name):Returns the attribute for the specified name.

5.

public void removeAttribute(String name):Removes the attribute with the given name from the
servlet context.

Usage of ServletContext Interface


There can be a lot of usage of ServletContext object. Some of them are as follows:
1.

The object of ServletContext provides an interface between the container and servlet.

2.

The ServletContext object can be used to get configuration information from the web.xml file.

3.

The ServletContext object can be used to set, get or remove attribute from the web.xml file.

4.

The ServletContext object can be used to provide inter-application communication.

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletGetAndPost</display-name>
<servlet>
<servlet-name>Manager</servlet-name>
<servlet-class>com.rks.servlet.Manager</servlet-class>
</servlet>
<context-param>
<param-name>year</param-name>
<param-value>2015</param-value>
</context-param>
<context-param>
<param-name>County</param-name>
<param-value>India</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Manager</servlet-name>
<url-pattern>/Manager</url-pattern>
</servlet-mapping>
</web-app>

package com.rks.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import
import
import
import
import

javax.servlet.ServletContext;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class Manager extends HttpServlet {


protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Inside doPost <br>");
/* Creating ServletConfig object */
ServletContext context = getServletConfig().getServletContext();
/* Another way to create Servlet Context object
ServletConfig context = getServletConfig();
ServletContext application = config.getServletContext();
*/
/*
* Method 1 : context.getInitParameter(String name);
* Return String
*/
String year = context.getInitParameter("year");
out.print("Year is : " +year);
/*
* Method 2 : context.getInitParameterNames();
* Return Enumeration
*/
Enumeration<Object> e = context.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String)e.nextElement();
out.print("<br>name : " + name);
String value = context.getInitParameter(name);
out.print("Value : " + value);
}
context.setAttribute("One", "One");
context.setAttribute("Two", "Two");

}
}

out.print("<a href='Controller'>Click</a>");
out.close();

package com.rks.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import
import
import
import
import

javax.servlet.ServletContext;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class Controller extends HttpServlet {


private static final long serialVersionUID = 1L;

protected void service(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();
String one = (String)context.getAttribute("One");
out.println("<br>One value is : " + one);
String two = (String)context.getAttribute("Two");
out.println("<br>Two value is : " + two);

}
}

context.removeAttribute("One");
String newOne = (String)context.getAttribute("One");
out.println("<br>After removal of : " + newOne);

Вам также может понравиться