Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of filters in Servlet

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    Servlet Filter:

    Servlet Filter is used to filter the unnecessary requests that is received at the servlet end. It is used to perform tasks such as conversion, logging, compression, encryption and decryption, input validation etc.

    It is called before and after the request is processed by the servlet.

    alt text

    Its uses:

    • It is used for logging purposes. All the incoming requests can be logged with the help of filter.
    • It can be used for security purposes by using encryption,decryption, etc.
    • Data compression, conversion.
    • Authentication of credentials.

    Advantages:

    • Less Maintenance cost. It is removable. We can insert as well as delete the entry from the deployment descriptor file(web.xml) as per our requirements.
    • They are independent. i.e. one filter don't have any dependency on another filter.

    There are three interfaces of Filter API:

    • Filter interface:

    A filter is created by implementing the filter interface. It provides the following lifecycle methods:

    • public void init(FilterConfig config): It is used to initialize the filter.

    • public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain): This is used for handling the requests for the entries mapped in the web.xml file . As soon as it receives the request it filters out the unnecessary requests.

    • public void destroy():This is invoked only once when filter is taken out of the service.

    • FilterChain interface:

    It is used for chaining purpose. i.e. it passes the object to the next filter or the next resource in the chain.

    There is one method in this:

    public void doFilter(HttpServletRequest request, HttpServletResponse response):it passes the control to the next filter or resource.

    • FilterConfig:

    The web container created it's object and is used to fetch the configuration information from the web.xml file.

    It has the following methods:

    public void init(FilterConfig config):This method is invoked only once and it is used to initialize the filter.

    public String getInitParameter(String parameterName):It returns the parameter value for the particular parameter name.

    public java.util.Enumeration getInitParameterNames():It gives an enumeration with the list of all the names.

    public ServletContext getServletContext(): It returns the ServletContext object.

    Filter mapping in web.xml file:

    alt text

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: