Servlet Context Attribute Problem Project

Click here to download eclipse supported ZIP file




 

    
package com.cv.servlet.context.attribute;

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;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ContextAttributeServlet
 
 @author Chandra Vardhan
 *
 */

public class ContextAttributeServlet extends HttpServlet {

  private static final Logger LOGGER = Logger
      .getLogger(ContextAttributeServlet.class);

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows IOException, ServletException {
    LOGGER.info("Entered into doGet(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet class... ");
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    LOGGER.info("Entered into doPost(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet class... ");
    PrintWriter pw = res.getWriter();
    res.setContentType("text/html");
    getServletContext().setAttribute("fooo""chandra");
    getServletContext().setAttribute("bar""Budwiser");
    try {
      Thread.sleep(5000);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    LOGGER.info("If you see fooo : 'chandra' and bar : 'Budwiser' then it is correct...");
    String foooVal = (StringgetServletContext().getAttribute("fooo");
    if (foooVal != null && foooVal.equalsIgnoreCase("chandra")) {
      LOGGER.info("fooo : 'chandra' and bar : 'Budwiser' you set here and those you got as output...");
      pw.print("<font color='GREEN'>fooo : 'chandra' and bar : 'Budwiser' you set here and those you got as output...</font><br/>");
    else {
      LOGGER.info("fooo : 'Vardhan' and bar : 'Calsberg' you set in ContextAttributeServlet2 program and those you got as output...");
      pw.print("<font color='RED'>fooo : 'Vardhan' and bar : 'Calsberg' you set in ContextAttributeServlet2 program and THIS IS WRONG output...</font><br/>");

    }
    pw.print("fooo attribute value is : " + foooVal+"<br/>");
    pw.print("bar attribute value is : "
        + getServletContext().getAttribute("bar"));

    pw.close();
  }
}


 

    
package com.cv.servlet.context.attribute;

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;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ContextAttributeServlet2
 
 
 @author Chandra Vardhan
 *
 */
public class ContextAttributeServlet2 extends HttpServlet {
  private static final Logger LOGGER = Logger
      .getLogger(ContextAttributeServlet2.class);

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows IOException, ServletException {
    LOGGER.info("Entered into doGet(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet2 class... ");
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    LOGGER.info("Entered into doPost(HttpServletRequest, HttpServletResponse) of ContextAttributeServlet2 class... ");
    PrintWriter pw = res.getWriter();
    res.setContentType("text/html");

    getServletContext().setAttribute("fooo""Vardhan");
    getServletContext().setAttribute("bar""Calsberg");

    pw.print("fooo attribute value is : " + getServletContext().getAttribute("fooo")+"<br/>");
    pw.print("bar attribute value is : "
        + getServletContext().getAttribute("bar"));
    pw.close();
  }
}



<!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>Context Attribute Servlet</title>
</head>
<body>
<br>
<br>
<center>
<form action="ContextAttributeServlet" method="GET" target="f1">
<input type="submit" value="First Click here...">
</form>
</center>
<br>
<br>
<center>
<form action="ContextAttributeServlet2" method="GET" target="f2">
<input type="submit" value="Immediately Click here...">
</form>
</center>
</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>ServletContextAttributeProblem</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.context.attribute</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"> <servlet> <servlet-name>ContextAttributeServlet</servlet-name> <servlet-class>com.cv.servlet.context.attribute.ContextAttributeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ContextAttributeServlet</servlet-name> <url-pattern>/ContextAttributeServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>ContextAttributeServlet2</servlet-name> <servlet-class>com.cv.servlet.context.attribute.ContextAttributeServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>ContextAttributeServlet2</servlet-name> <url-pattern>/ContextAttributeServlet2</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.html</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment