Servlet Default Headers Information Project

Click here to download eclipse supported ZIP file




 

    
package com.cv.servlet.header;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
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 HeaderServlet
 
 @author Chandra Vardhan
 
 */
public class HeaderServlet extends HttpServlet {
  private static final Logger LOGGER = Logger.getLogger(HeaderServlet.class);

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows IOException, ServletException {
    LOGGER.info("LOGGER : HeaderServlet doGet(HttpServletRequest,HttpServletResponse)");
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    LOGGER.info("LOGGER : HeaderServlet doPost(HttpServletRequest,HttpServletResponse)");
    response.setContentType("text/html");
    RequestDispatcher view = request.getRequestDispatcher("result.jsp");
    view.forward(request, response);
  }
}


 

    
package com.cv.servlet.header;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet Filter implementation class RequestParameterChangeFilter
 
 @author Chandra Vardhan
 
 */

public class ResponseChangeFilter implements Filter {

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

  private ServletContext context;

  public void init(FilterConfig fConfigthrows ServletException {
    LOGGER.info("init ... ResponseChangeFilter");
    this.context = fConfig.getServletContext();
    this.context.log("ResponseChangeFilter initialized");
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chainthrows IOException, ServletException {
    LOGGER.info("doFilter ... ResponseChangeFilter BEFORE filter");

    HttpServletResponse httpServletResponse =((HttpServletResponseresponse);
    httpServletResponse.setHeader("foo""bar");
    chain.doFilter(request, httpServletResponse);

    LOGGER.info("AFTER filter, original response: " );
  }

  public void destroy() {
    // we can close resources here
    LOGGER.info("destroy ... RequestParameterChangeFilter");
  }

}



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<br>
<br>
<form action="HeaderServlet" method="POST" id='test'>
<fieldset>
<legend> Welcome to the Header Servlet project... Page 1</legend>
<table align="center">
<tr>
<td align="right"><input type="submit" value="Submit" /></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>



<%@ page import="java.util.*"%>
<html>
<body>
<h2 align="center">Headers Information Servlet !!!</h2>
<p>
<%
  Enumeration<String> headers = request.getHeaderNames();
  
  while(headers.hasMoreElements()) {
  out.print("<br>header:" + request.getHeader(headers.nextElement()));
  }
  
  %>
</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>ServletDefaultHeadersInfoProject</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>HeaderServlet</servlet-name> <servlet-class>com.cv.servlet.header.HeaderServlet</servlet-class> <init-param> <param-name>n1</param-name> <param-value> 100 </param-value> </init-param> <init-param> <param-name>n2</param-name> <param-value> 200 </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HeaderServlet</servlet-name> <url-pattern>/HeaderServlet</url-pattern> </servlet-mapping> <filter> <filter-name>TestFilter</filter-name> <filter-class>com.cv.servlet.header.ResponseChangeFilter</filter-class> </filter> <filter-mapping> <filter-name>TestFilter</filter-name> <url-pattern>/HeaderServlet</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>first.jsp</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment