Servlet Session Attribute Solution Project

Click here to download eclipse supported ZIP file




 

    
package com.cv.servlet.session.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 javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

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

public class ServletSessionAttributeServlet extends HttpServlet {
  private static final Logger LOGGER = Logger.getLogger(ServletSessionAttributeServlet.class);

  /**
   * Constructor of the object.
   */
  public ServletSessionAttributeServlet() {
    super();
  }

  /**
   * Destruction of the servlet. <br>
   */
  public void destroy() {
    super.destroy()// Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to get.
   
   @param request
   *            the request send by the client to the server
   @param response
   *            the response send by the server to the client
   @throws ServletException
   *             if an error occurred
   @throws IOException
   *             if an error occurred
   */
  public void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doGet(HttpServletRequest ,HttpServletResponse ) of ServletSessionAttributeServlet class... ");

    doPost(request, response);
  }

  /**
   * The doPost method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to
   * post.
   
   @param request
   *            the request send by the client to the server
   @param response
   *            the response send by the server to the client
   @throws ServletException
   *             if an error occurred
   @throws IOException
   *             if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
    
    
    LOGGER.info("Entered into doPost(HttpServletRequest ,HttpServletResponse ) of ServletSessionAttributeServlet 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>");
    out.print("    This is ");

    HttpSession httpSession = request.getSession();

    synchronized (httpSession) {
      httpSession.setAttribute("bar""22");
      LOGGER.info("httpSession.setAttribute('bar', '22')... ");
      httpSession.setAttribute("foo""42");
      LOGGER.info("httpSession.setAttribute('foo', '42')... ");
      try {
        Thread.sleep(5000);
      catch (Exception e) {
        e.printStackTrace();
      }
      Object barAttribute = httpSession.getAttribute("bar");
      Object fooAttribute = httpSession.getAttribute("foo");
      out.print("<font size=5> If you get the content in 'GREEN' color then that is correct... If you get 'RED' then that is WRONG...</font><br/>");
      if("22".equalsIgnoreCase(barAttribute.toString())) {
      out.print("<font color='GREEN'>The value of 'bar' attribute is : "+barAttribute+"</font><br/>");
      LOGGER.info("httpSession.getAttribute('bar')..."+barAttribute);
      out.print("<font color='GREEN'>The value of 'foo' attribute is : "+fooAttribute+"</font><br/>");
      LOGGER.info("httpSession.getAttribute('foo')..."+fooAttribute);
      else {
        out.print("<font color='RED'>The value of 'bar' attribute is : "+barAttribute+"</font><br/>");
        LOGGER.info("httpSession.getAttribute('bar')..."+barAttribute);
        out.print("<font color='RED'>The value of 'foo' attribute is : "+fooAttribute+"</font><br/>");
        LOGGER.info("httpSession.getAttribute('foo')..."+fooAttribute);
      }
      
    }    
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
  }

  /**
   * Initialization of the servlet. <br>
   *
   @throws ServletException
   *             if an error occurs
   */
  public void init() throws ServletException {
    // Put your code here
  }

}


 

    
package com.cv.servlet.session.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 javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

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

public class ServletSessionAttributeServlet2 extends HttpServlet {
  private static final Logger LOGGER = Logger.getLogger(ServletSessionAttributeServlet.class);

  /**
   * Constructor of the object.
   */
  public ServletSessionAttributeServlet2() {
    super();
  }

  /**
   * Destruction of the servlet. <br>
   */
  public void destroy() {
    super.destroy()// Just puts "destroy" string in log
    // Put your code here
  }

  /**
   * The doGet method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to get.
   
   @param request
   *            the request send by the client to the server
   @param response
   *            the response send by the server to the client
   @throws ServletException
   *             if an error occurred
   @throws IOException
   *             if an error occurred
   */
  public void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info(
        "Entered into doGet(HttpServletRequest ,HttpServletResponse ) of ServletSessionAttributeServlet2 class... ");

    doPost(request, response);
  }

  /**
   * The doPost method of the servlet. <br>
   *
   * This method is called when a form has its tag value method equals to
   * post.
   
   @param request
   *            the request send by the client to the server
   @param response
   *            the response send by the server to the client
   @throws ServletException
   *             if an error occurred
   @throws IOException
   *             if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {

    LOGGER.info(
        "Entered into doPost(HttpServletRequest ,HttpServletResponse ) of ServletSessionAttributeServlet2 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>");

    HttpSession httpSession = request.getSession();
    synchronized (httpSession) {
      httpSession.setAttribute("bar""82");    
      LOGGER.info("httpSession.setAttribute('bar', '82')... ");
      httpSession.setAttribute("foo""88");
      LOGGER.info("httpSession.setAttribute('foo', '88')... ");    
      Object barAttribute = httpSession.getAttribute("bar");
      Object fooAttribute = httpSession.getAttribute("foo");
      out.print("<font size=5>The value of 'bar' attribute is : "+barAttribute+"</font><br/>");
      LOGGER.info("httpSession.getAttribute('bar')..."+barAttribute);
      out.print("<font size=5>The value of 'foo' attribute is : "+fooAttribute+"</font><br/>");
      LOGGER.info("httpSession.getAttribute('foo')..."+fooAttribute);
    }
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
  }

  /**
   * Initialization of the servlet. <br>
   *
   @throws ServletException
   *             if an error occurs
   */
  public void init() throws ServletException {
    // Put your code here
  }

}



<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Servlet Session Attribute Servlet</title>
</head>
<body>
<center>
Click this below button first.
<form method="post" action="ServletSessionAttributeServlet" target="f1">
<br />
<br /> <input type="submit" value="Click this first..." />
</form>
</center>
<br />
<br />
<center>
Click this below button next after 2 seconds...
<form method="post" action="ServletSessionAttributeServlet2" target="f2">
<br />
<br /> <input type="submit" value="Click this next..." />
</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>ServletSessionAttributeSolution</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.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>ServletSessionAttributeServlet</servlet-name> <servlet-class>com.cv.servlet.session.attribute.ServletSessionAttributeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletSessionAttributeServlet</servlet-name> <url-pattern>/ServletSessionAttributeServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>ServletSessionAttributeServlet2</servlet-name> <servlet-class>com.cv.servlet.session.attribute.ServletSessionAttributeServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletSessionAttributeServlet2</servlet-name> <url-pattern>/ServletSessionAttributeServlet2</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.jsp</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment