Pages

Saturday, June 30, 2012

ServletConfig and ServletContext in Java Servlets

ServletConfig
***************
ServletConfig encapsulates servlet configuration,that is used by the servlet container,used to pass information to servlet during initialix(z)ation.
ServletConfig is one per servlet.

Methods in <<ServletConfig>> interface:
i)          String getInitParameter(String)
ii)          Enumeration getInitParameterNames()
iii)         ServletContext getServletContext()
iv)         String getServletName() 

ServletContext
*****************
ServletContext is one per web application.

Methods in <<ServletContext>> interface:
i)          Object getAttribute(String)
ii)          String getInitParameter(String)
iii)         String getServletContextName()
iv)         void setAttribute(String,Object)
v)         void removeAttribute(String)
vi)         RequestDispatcher getRequestDispatcher(String path)
and many more...

A simple example
showing how to retrieve init and context parameters:

web.xml
<?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>webAppSamples</display-name>
  <servlet>
    <description></description>
    <display-name>TestInitParam</display-name>
    <servlet-name>TestInitParam</servlet-name>
    <servlet-class>foo.TestInitParam</servlet-class>
    <init-param> <!-- Servlet init parameters -->
     <param-name>email</param-name>
     <param-value>slnv@gmail.com</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestInitParam</servlet-name>
    <url-pattern>/TestInitParam</url-pattern>
  </servlet-mapping>
  <context-param> <!-- Context init parameters -->
    <param-name>email</param-name>
    <param-value>praveen@yahoo.com</param-value>
  </context-param>
</web-app>

TestInitParam.java
package foo;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class TestInitParam
 * @author S L N V Praveen
 */
public class TestInitParam extends HttpServlet {
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Servlet Name : " getServletName() "<br>");
  out.println("init param : " getServletConfig().getInitParameter("email") "<br>");
  out.println("context param : " getServletContext().getInitParameter("email") "<br>");
  out.println("context param : " getServletConfig().getServletContext().getInitParameter("email") "<br>"); //other way to retrieve context                        //parameters
  out.println("context param : " this.getServletContext().getInitParameter("email") "<br>"); //other way to retrieve context parameters
  
 }
}

No comments:

Post a Comment