Servlet Filters Execution Project

Click here to download eclipse supported ZIP file




 

    

package com.cv.servlet.filter.execution;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet Filter implementation class Filter1
 
 @author Chandra Vardhan
 
 */
public class Filter1 implements javax.servlet.Filter {
  public FilterConfig filterConfig; // 1
  private static final Logger LOGGER = Logger
      .getLogger(Filter1.class);


  public void doFilter(final ServletRequest request, // 2
      final ServletResponse response, FilterChain chain)
      throws java.io.IOException, javax.servlet.ServletException {
    
    LOGGER.info("LOGGER : Before doFilter(ServletRequest, ServletResponse, FilterChain) Filter1 class...");
    chain.doFilter(request, response)// 3
    LOGGER.info("LOGGER : After doFilter(ServletRequest, ServletResponse, FilterChain) Filter1 class...");
  }

  public void init(final FilterConfig filterConfig) { // 4
    this.filterConfig = filterConfig;
  }

  public void destroy() { // 5
  }
}


 

    

package com.cv.servlet.filter.execution;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet Filter implementation class Filter2
 
 @author Chandra Vardhan
 
 */
public class Filter2 implements javax.servlet.Filter {
  public FilterConfig filterConfig; // 1
  private static final Logger LOGGER = Logger
      .getLogger(Filter2.class);

  public void doFilter(final ServletRequest request, // 2
      final ServletResponse response, FilterChain chain)
      throws java.io.IOException, javax.servlet.ServletException {
    LOGGER.info("LOGGER : Before doFilter(ServletRequest, ServletResponse, FilterChain) Filter2 class...");
    chain.doFilter(request, response)// 3
    LOGGER.info("LOGGER : After doFilter(ServletRequest, ServletResponse, FilterChain) Filter2 class...");
  }

  public void init(final FilterConfig filterConfig) { // 4
    this.filterConfig = filterConfig;
  }

  public void destroy() { // 5
  }
}


 

    

package com.cv.servlet.filter.execution;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet Filter implementation class Filter3
 
 @author Chandra Vardhan
 
 */
public class Filter3 implements javax.servlet.Filter {
  public FilterConfig filterConfig; // 1
  private static final Logger LOGGER = Logger
      .getLogger(Filter3.class);

  public void doFilter(final ServletRequest request, // 2
      final ServletResponse response, FilterChain chain)
      throws java.io.IOException, javax.servlet.ServletException {
    LOGGER.info("LOGGER : Before doFilter(ServletRequest, ServletResponse, FilterChain) Filter3 class...");
    chain.doFilter(request, response)// 3
    LOGGER.info("LOGGER : After doFilter(ServletRequest, ServletResponse, FilterChain) Filter3 class...");
  }

  public void init(final FilterConfig filterConfig) { // 4
    this.filterConfig = filterConfig;
  }

  public void destroy() { // 5
  }
}


 

    
package com.cv.servlet.filter.execution;

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 Filter implementation class TestServlet
 
 @author Chandra Vardhan
 
 */
public class TestServlet extends HttpServlet {

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

  private static final long serialVersionUID = 1L;

  /**
   @see HttpServlet#HttpServlet()
   */
  public TestServlet() {
    super();
  }

  /**
   @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("LOGGER : TestServlet doGet(HttpServletRequest,HttpServletResponse)");
    LOGGER.info("LOGGER : Provided output Test chandra vardhan kodam");
    PrintWriter out = response.getWriter();

    out.print("Test chandra vardhan kodam");

  }

  /**
   @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("LOGGER : TestServlet doPost(HttpServletRequest ,HttpServletResponse )");
    doGet(request, response);
  }

}



<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<br>
<center><a href="TestServlet/cv.do">Click here to get response...</a></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>ServletFiltersExecutionProject</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.filter.response</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>TestServlet</servlet-name> <servlet-class>com.cv.servlet.filter.execution.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet/*</url-pattern> </servlet-mapping> <filter> <filter-name>Filter1</filter-name> <filter-class>com.cv.servlet.filter.execution.Filter1</filter-class> <init-param> <param-name>message</param-name> <param-value>Hello!</param-value> </init-param> </filter> <filter> <filter-name>Filter2</filter-name> <filter-class>com.cv.servlet.filter.execution.Filter2</filter-class> </filter> <filter> <filter-name>Filter3</filter-name> <filter-class>com.cv.servlet.filter.execution.Filter3</filter-class> </filter> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/TestServlet/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter2</filter-name> <url-pattern>/TestServlet/cv.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Filter3</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment