Servlet Init And Context Parameters Project

Click here to download eclipse supported ZIP file




 

    
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 = (StringcontextParameterNames.nextElement();
      initParams.put(paramName, getServletContext().getInitParameter(paramName));
    }
    
    Enumeration initParameterNames = getServletConfig().getInitParameterNames();
    while (initParameterNames.hasMoreElements()) {
      String paramName = (StringinitParameterNames.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);
  }

}



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beer title here</title>
</head>
<body>
<form action="InitContextParameterServlet" method="post">
<input type="submit" value="Click here to continue...">
</form>
</body>
</html>



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>ServletInitAndContextParametersProject</artifactId> <version>1.0</version> <packaging>war</packaging> <properties> <log4j.version>1.2.16</log4j.version> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <groupId>com.cv.servlet.session.listener</groupId> </project>




 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextParam</param-name> <param-value>contextParamValue</param-value> </context-param> <context-param> <param-name>contextParam2</param-name> <param-value>contextParamValue2</param-value> </context-param> <context-param> <param-name>contextParam3</param-name> <param-value>contextParamValue3</param-value> </context-param> <servlet> <servlet-name>InitContextParameterServlet</servlet-name> <servlet-class>com.cv.servlet.init.context.param.InitContextParameterServlet</servlet-class> <init-param> <param-name>initParam</param-name> <param-value>initParamValue</param-value> </init-param> <init-param> <param-name>initParam2</param-name> <param-value>initParamValue2</param-value> </init-param> <init-param> <param-name>initParam3</param-name> <param-value>initParamValue3</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>InitContextParameterServlet</servlet-name> <url-pattern>/InitContextParameterServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.html</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment