package com.cv.servlet.init.context.param;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
* @author Chandra Vardhan
*
*/
public class InitContextParameterServlet extends HttpServlet {
private static final Logger LOGGER = Logger
.getLogger(InitContextParameterServlet.class);
private static Map<String, String> initParams = new HashMap<String, String>();
int count = 0;
/**
* Constructor of the object.
*/
public InitContextParameterServlet() {
super();
}
public void init() throws ServletException {
LOGGER.info("Entered into init() of InitParameterServlet class... ");
// This is reading all the init Parameters at once...
Enumeration contextParameterNames = getServletContext().getInitParameterNames();
while (contextParameterNames.hasMoreElements()) {
String paramName = (String) contextParameterNames.nextElement();
initParams.put(paramName, getServletContext().getInitParameter(paramName));
}
Enumeration initParameterNames = getServletConfig().getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String paramName = (String) initParameterNames.nextElement();
initParams.put(paramName, getServletConfig().getInitParameter(paramName));
}
LOGGER.info("Params from init() : " + initParams);
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
LOGGER.info("Entered into destroy() of InitContextParameterServlet class... ");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOGGER.info("Entered into doGet(HttpServletRequest, HttpServletResponse) of InitContextParameterServlet class... ");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
String contextParameterValue1 = getServletContext().getInitParameter(
"contextParam");
String contextParameterValue2 = getServletContext().getInitParameter(
"contextParam2");
String contextParameterValue3 = getServletContext().getInitParameter(
"contextParam3");
LOGGER.info("This is reading init Parameters one by one..." );
LOGGER.info("contextParam : " + contextParameterValue1 );
LOGGER.info("contextParam2 : " + contextParameterValue2 );
LOGGER.info("contextParam3 : " + contextParameterValue3 );
out.print("This is reading init Parameters one by one..." + "<br>");
out.print("contextParam : " + contextParameterValue1 + "<br>");
out.print("contextParam2 : " + contextParameterValue2 + "<br>");
out.print("contextParam3 : " + contextParameterValue3 + "<br>");
String initParameterValue1 = getServletConfig().getInitParameter(
"initParam");
String initParameterValue2 = getServletConfig().getInitParameter(
"initParam2");
String initParameterValue3 = getServletConfig().getInitParameter(
"initParam3");
LOGGER.info("This is reading init Parameters one by one..."+ "<br>");
LOGGER.info("initParam : "+initParameterValue1+ "<br>");
LOGGER.info("initParam2 : "+initParameterValue2+ "<br>");
LOGGER.info("initParam3 : "+initParameterValue3+ "<br>");
out.print("This is reading init Parameters one by one..."+ "<br>");
out.print("initParam : "+initParameterValue1+ "<br>");
out.print("initParam2 : "+initParameterValue2+ "<br>");
out.print("initParam3 : "+initParameterValue3+ "<br>");
out.print("This is reading all the init Parameters at once..." + "<br>");
for (Map.Entry<String, String> initParamMap : initParams.entrySet()) {
out.print(initParamMap.getKey() + " : " + initParamMap.getValue()
+ "<br>");
LOGGER.info(initParamMap.getKey() + " : " + initParamMap.getValue());
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOGGER.info("Entered into doPost(HttpServletRequest, HttpServletResponse) of InitContextParameterServlet class... ");
doGet(request, response);
}
} |
No comments:
Post a Comment