Servlets - SPLessons

Servlet FilterConfig

Chapter 37

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet FilterConfig

Servlet FilterConfig

shape Description

Servlet FilterConfig, Servlet Filters are special web resource programs of the web application to trap and take the request and response of other web resource programs. If needed to add any new functionality or any code without disturbing the source code of the existing web resource program, servlet filter program will help there. Servlet program can be linked from the filter program by using a URL pattern in the web.xml file. That means a URL pattern of servlet program and URL pattern of filter program must be same. Then only the filter is invoked in servlet program. Servlet filter program is implemented Java.servlet.Filter interface. By using filter and filter mapping tags, web.xml file must be configured.

The Servlet FilterConfig is used to get information from the web.xml and FilterConfig is created by the web container. Following are the methods of FilterConfig.

A web container is the segment of a web server that cooperates with Java servlets. A web container is the responsible of dealing with the lifecycle of servlets, mapping a URL to a specific servlet and guaranteeing that the URL requester has the right get to rights.


shape Examples

Example for Servlet FilterConfig.

index.html

<html>

<form action="./welcome">
		username:<input type = "text" name = "userName"/></br></br>
		<input type = "submit" value="Login to SPlessons"/>
	</form>

 </html>   

web.xml

<web-app>  
   
<servlet>  
<servlet-name>servlet</servlet-name>  
<servlet-class>servletfilter.DemoServletFilters</servlet-class>  
</servlet>  
   
<servlet-mapping>  
<servlet-name>servlet</servlet-name>  
<url-pattern>/welcome</url-pattern>  
</servlet-mapping>  
   
<filter>  
<filter-name>filter</filter-name>  
<filter-class>servletfilter.MyFilter</filter-class>  
</filter>  
    
<filter-mapping>  
<filter-name>filter</filter-name>  
<url-pattern>/welcome</url-pattern>  
</filter-mapping>  

</web-app>  

Following is the web.xml pattern when using filters.

MyFilter.java

package servletfilter;

import java.io.IOException;  
import java.io.PrintWriter;  
   
import javax.servlet.*;  
   
public class MyFilter implements Filter{  
   
public void init(FilterConfig arg0) throws ServletException {
    //initialization of filter
}  
       
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
           
    PrintWriter out=response.getWriter();  
    String userName = request.getParameter("userName");
    out.print("<h2>Hello "+userName+"</h2>");  
           
    chain.doFilter(request, response);//sends request to next resource  
           
    out.print("<h2>Thank you "+userName+"</h2>");  
    }  
    public void destroy() {
    //servlet filter destroy
    }  
}  

public void init(FilterConfig config) is invoked only once it is used to initialize the filter. The doFilter technique for the Filter is called by the container every time a request/response pair is gone through the chain because of a client request for an asset toward the end of the chain. All filters are chained (in the order of their definition in web.xml). The chain.doFilter() is proceeding to the next element in the chain. The last element of the chain is the target resource/servlet. public void destroy() is invoked only once when filter is taken out of the service.
DemoServletFilters.java

import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
  
public class DemoServletFilters extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
  
        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
      
        out.print("


<h2>welcome to Splessons ServletFilters</h2>



");  
          
    }  
  
}  

Output
By compiling the code one can get the following output as shown below, enter the username and add button.

By clicking on the button following message will be displayed.

FilterConfig

shape Description

The FilterConfig is used to get information from the web.xml and FilterConfig is created by the web container. Following are the methods of FilterConfig.


shape Examples

Example on FilterConfig:
index.html

<html>
		<form action="./welcome">
				<input type="submit" value="click here"/>
		</form>
</html>

web.xml

<web-app>  
  
 <servlet>  
    <servlet-name>Servlet</servlet-name>  
    <servlet-class>DemoFilterConfig</servlet-class>  
  </servlet>  
  
  <servlet-mapping>  
    <servlet-name>Servlet</servlet-name>  
    <url-pattern>/welcome</url-pattern>  
  </servlet-mapping>  
    
  <filter> 
   
  <filter-name>filter</filter-name>  
  <filter-class>MyFilterConfig</filter-class>  
  
  <init-param>  
  <param-name>user_name</param-name>  
  <param-value>Enni</param-value>  
  </init-param>
    
  </filter>  
  
  <filter-mapping>  
  <filter-name>filter</filter-name>  
  <url-pattern>/welcome</url-pattern>  
  </filter-mapping>  
    
    
</web-app>  

MyFilterConfig.java

import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.*;  
  
public class MyFilterConfig implements Filter{  
FilterConfig config;  
  
public void init(FilterConfig config) throws ServletException {  
    this.config=config;  
}  
  
public void doFilter(ServletRequest request, ServletResponse response,  
    FilterChain chain) throws IOException, ServletException {  
      
    PrintWriter out=response.getWriter();  
          
    String userName=config.getInitParameter("user_name");//get param-value from web.xml by call param-name 
          
    out.print("


<h2>Hello "+userName+", your name is get from web.xml Filter tag</h2>



");  
    
    chain.doFilter(request, response);//sends request to next resource  
          
    out.print("


<h2>Thank you "+userName+"</h2>



");      
          
}  
public void destroy() {}  
}  

In doGet(), the parameters are appended to the URL and sent along with the header information. Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification.
DemoFilterConfig.java

import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
  
public class DemoFilterConfig extends HttpServlet {  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
  
        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
      
        out.print("<h2>welcome to Splessons ServletFilterConfig</h2>");  
          
}  
  
}  

Output
Click on the button from the generated output.

Result will be as follows.

Summary

shape Key Points

  • Servlet FilterConfig – The filter is an object and pluggable.
  • The filter gives less maintenance.
  • Servlet FilterConfig – Filter data will be also placed in web.xml .