FilterConfig Interface

The FilterConfig interface
public interface FilterConfig : This interface is the equilvalent of ServletConfig and its is used for filters. The interface provides access to the filter.
Method of FilterConfig interface
  1. public void getFilterName():
               This method returns the name of filter as assgined in deployment descriptor.
  2. public String getInitParamter(String name):
              This method returns the value of the named initilization parameters or null if the parameter does not exist.
  3. public Enumeration getInitParamterNames():
              This method returns the name of Servlet initilizatiion parameters as an Enumeration of String objects, or an empty Enumeratio of the Servlet has no initilizaion parameters.
  4. public String getServletContex():
               This method returns a reference to the ServletContext object associated with the web application.
Example of FilterConfig interface
index.html
<html> <head> <title>Filter Configuration Example </title> </head> <body> <center> <form action="WelcomeServlet" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </center> </body> </html>
LoginFilter.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class LoginFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig=filterConfig; } FilterConfig filterConfig=null; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); String adminName,adminPassword; adminName = filterConfig.getInitParameter("adminName"); adminPassword = filterConfig.getInitParameter("adminPassword"); String userName = request.getParameter("userName"); String password = request.getParameter("password"); if(userName.equals(adminName)&& password.equals(adminPassword)) { out.println("Authorised User"); out.println("<BR>"); chain.doFilter(request, response); } else { out.println("<font color='#FF0000'>"+ "Login failed. Please try again.</font><BR>"); RequestDispatcher rd = request.getRequestDispatcher("/index.html"); rd.include(request, response); } } public void destroy() { this.filterConfig = null; } }
WelcomeServlet.java
import java.io.PrintWriter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WelcomeServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("userName"); String password=request.getParameter("password"); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Welcome Page </TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("User Name is "+name); out.println("<BR>"); out.println("Password is "+password); out.println("<P>Welcome to the nsstudent.com.</P>"); out.println("</BODY>"); out.println("</HTML>"); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <display-name>LoginFilter</display-name> <filter-name>LoginFilter</filter-name> <filter-class>LoginFilter</filter-class> <init-param> <param-name>adminName</param-name> <param-value>Sunil</param-value> </init-param> <init-param> <param-name>adminPassword</param-name> <param-value>Kumar</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/WelcomeServlet</url-pattern> </filter-mapping> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> </web-app>
Output
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE
 



 

Latest Tutorial


Most Visited Tutorial