Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for accessing first filter in Servlet
Previous Home Next

In this Servlet application, a javax.Servlet.Filter capability adding it into application. Filter is used for examining client request and invoke resource respectively, modify client request and response before sending it to client.Consider following steps for creating this application-

Step-1 Create a new web project and name it as FilterServlet. Go to source package and name it as r4r.Servlet.Filter, now right click over package and go to New -> other ->New File, window open (as show into below picture). Now choose Filter form the option list and click Next button.

Step-2 After hit next button Filter window open. Specify the values as shown below picture and click on Next button for mapping this into web.xml file. Remember : Wrap Request and Response Object provide HTTP-specific capabilities into application.

Step-3 Now configure Filter into web.xml file by tick check into check box. Now click New button for mapping Filter mapping into web.xml file (as show into below picture).

Step-4 If any parameters add into filter the click Next button else click Finish button, filter configuration add into web.xml file.

Application directory structure

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
  xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <filter>
  <filter-name>R4RFilter</filter-name>
  <filter-class>r4r.Servlet.Filter.R4RFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>R4RFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <session-config>
  <session-timeout>
    30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
 Document: index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>r4r.co.in-index</title>
 </head>
 <body>
  <h1>Invoke javax.servlet.Filter into application!</h1>
 </body>
</html>

Filter Program

/*
 * Save as a R4RFilter.java
 */
package r4r.Servlet.Filter;

import java.io.IOException;
import java.text.*;
import java.util.Calendar;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author R4R
 */
public class R4RFilter implements javax.servlet.Filter {

 private static final boolean debug = true;
 private FilterConfig filterConfig = null;
 //Create a custom date and time
 private DateFormat dateFormat;
 private String newDate;

public R4RFilter() {
 }

 /**
  * Init method for this filter use for initialize parameter
  */
 @Override
 public void init(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
  if (filterConfig != null) {
if (debug) {
 log("R4RFilter:Initializing filter");
}
  }
  dateFormat = new SimpleDateFormat("dd/mm/yyyy");
  newDate = dateFormat.format(Calendar.getInstance().getTime());
 }

 @Override
 public void doFilter(ServletRequest request,
	  ServletResponse response, FilterChain chain)
throws IOException, ServletException {
  HttpServletRequest req = (HttpServletRequest) request;
  if (debug) {
log("R4RFilter:doFilter()");
  }
  //write massage over mapping URL in xml file
  response.getWriter().write(req.getHeader("user-agent"));
  response.getWriter().write
	  ("<BR>Message send from filter through " + "<b><I>"
 + req.getRequestURL() + "</I></b>" +
	        " On " + "<b>" + newDate + "<b>");
  chain.doFilter(request, response);
 }

 /**
  * Return the filter configuration object for this filter.
  */
 public FilterConfig getFilterConfig() {
  return (this.filterConfig);
 }

 /**
  * Set the filter configuration object for this filter.
  */
 public void setFilterConfig(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
 }

 /**
  * Destroy method for this filter 
  */
 @Override
 public void destroy() {
 }
 /*
  * log method is used for print message over server console
  */

 public void log(String msg) {
  filterConfig.getServletContext().log(msg);
 }
}
Output of Program
Previous Home Next