CodesJava

Easy learning with example program codes

FilterConfig interface


FilterConfig object is created and used by web container to pass init parameters to a filter during initialization.

Methods of FilterConfig interface:

1. getFilterName(): Returns the name of the filter defined in web.xml.

Syntax: public String getFilterName() 

2. getInitParameter(String name): Returns the value of the specified parameter.

Syntax: public String getInitParameter(String name) 

3. getInitParameterNames(): Returns the names of all parameters as Enumeration.

Syntax:public Enumeration getInitParameterNames() 

4. getServletContext(): Returns the object of ServletContext.

Syntax: public ServletContext getServletContext()

Example:

MyFilter.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;
 
/**
* This class is used as a filter.
* @author codesjava
*/
public class MyFilter implements Filter {
	private static final long serialVersionUID = 1L;
	private FilterConfig filterConfig;
 
	public void init(FilterConfig filterConfig) 
	                         throws ServletException {
		this.filterConfig = filterConfig;
	}  
 
	public void doFilter(ServletRequest request, 
			ServletResponse response,FilterChain chain) 
	                   throws IOException, ServletException {
 
 
	response.setContentType("text/html"); 
    	PrintWriter out = response.getWriter();
 
    	//get parameters from filterConfig object.
    	String appUser = filterConfig.getInitParameter("appUser");  
    	if(appUser.equals("jai")){
    		chain.doFilter(request, response);
    	}
    	else {
    		out.print("Invalid application user.");
    	}	          
    }  
 
    public void destroy() {
 
    }  
}

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;
 
/**
 * This class is used to print login 
 * message if user logged in successfully.
 */
public class WelcomeServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
 
   //no-argument constructor
    public WelcomeServlet() {
 
    }
 
    protected void doGet(HttpServletRequest request, 
			HttpServletResponse response)
	               throws ServletException, IOException {
		response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
 
        out.print("<h1>Valid application user.</h1>");  
        out.close();  
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
 
  <servlet>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>
    	com.codesjava.business.WelcomeServlet
    </servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/WelcomeServlet</url-pattern>
  </servlet-mapping>
 
  <filter>  
  	<filter-name>MyFilter</filter-name>  
  	<filter-class>
  		com.codesjava.business.MyFilter
  	</filter-class> 
  	<init-param>
  	  <param-name>appUser</param-name>
  	  <param-value>jai</param-value>
  	</init-param> 
  </filter>  
 
  <filter-mapping>  
  	<filter-name>MyFilter</filter-name>  
  	<url-pattern>/WelcomeServlet</url-pattern>  
  </filter-mapping>  
 
</web-app>

Output:

servlet example 15
 
Download this example.
 
Next Topic: JSP Tutorial with examples.
Previous Topic: Servlet filter in java with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki