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()


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 {
    	PrintWriter out = response.getWriter();
    	//get parameters from filterConfig object.
    	String appUser = filterConfig.getInitParameter("appUser");  
    		chain.doFilter(request, response);
    	else {
    		out.print("Invalid application user.");
    public void destroy() {

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 {
        PrintWriter out = response.getWriter();  
        out.print("<h1>Valid application user.</h1>");  


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
id="WebApp_ID" version="2.5">


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.


Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki