Saturday, 23 August 2014

init (FilterConfig filterConfig)

public void init(FilterConfig filterConfig) throws ServletException
init method is called by the web container to indicate to a filter that it is being placed into service. The init method must complete successfully before the filter is asked to do any filtering work.

The web container cannot place the filter into service if the init method either
a. Throws a ServletException
b. Does not return within a time period defined by the web container

Only one instance per <filter> declaration in the deployment descriptor is
instantiated per JVM of the container.

You can initialize parameters to a filter in deployment descriptor.

 <filter>
        <filter-name>FilterInit</filter-name>
        <filter-class>FilterInit</filter-class>
         <init-param>
            <param-name>contact</param-name>
            <param-value>abcabc@gmail.com</param-value>
        </init-param>
</filter>

main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   
     <title>Filter Example</title>
    </head>
    <body>
        <form method="post" action="/servlet/Home">
            <input type="text" name="userName">
            <input type="submit" value ="Get Data">
        </form>
    </body>
</html>

FilterInit.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class FilterInit implements Filter {
    
    String mailId;
    
    @Override
   public void init(FilterConfig filterConfig) throws ServletException{
       mailId = filterConfig.getInitParameter("contact");
   }
   
   @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException{
        try(PrintWriter out = res.getWriter()){
          String userName = req.getParameter("userName");
          if(userName.equals("blockme")){
              out.println("Unable to process your request, contact " + mailId);
          }
          else{
              out.println("FilterInit forwarding request");
              chain.doFilter(req, res);
          }
      }
    }
    
   @Override
    public void destroy(){
        
    } 
}


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <filter>
        <filter-name>FilterInit</filter-name>
        <filter-class>FilterInit</filter-class>
         <init-param>
            <param-name>contact</param-name>
            <param-value>abcabc@abcdef.com</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>FilterInit</filter-name>
        <url-pattern>/Home</url-pattern>
    </filter-mapping>
    
    <servlet>
        <servlet-name>Welcome</servlet-name>
        <servlet-class>Welcome</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Welcome</servlet-name>
        <url-pattern>/Home</url-pattern>
    </servlet-mapping>
</web-app>

Welcome.java
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;

public class Welcome extends HttpServlet {
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
      try(PrintWriter out = res.getWriter()){
          String userName = req.getParameter("userName");
          out.println("Welcome " + userName);
      }
  }
}


Output








Prevoius                                                 Next                                                 Home

No comments:

Post a Comment