Books Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Latest:- Important tips for Campus Placements
0 like 0 dislike
292 views
in RTU B.Tech (CSE-V Sem) Advance Java Lab by Goeduhub's Expert (5.8k points)

Introduction to Servlet Filter with simple Filter Application

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
selected by
 
Best answer

Servlet Filter

  • A filter is an object that is invoked at the pre-processing and post-processing of a request.
  • It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.
  • The servlet filter is pluggable, i.e. its entry is defined in the web.xml file, if we remove the entry of filter from the web.xml file, filter will be removed automatically and we don't need to change the servlet.

ServletFilter

Usage of Filter

  • recording all incoming requests
  • logs the IP addresses of the computers from which the requests originate
  • conversion
  • data compression
  • encryption and decryption
  • input validation etc.

Advantages of Filter

  • Filter is pluggable.
  • One filter don't have dependency onto another resource.
  • Less Maintenance

Filter API

The javax.servlet package contains the three interfaces of Filter API.

  • Filter
  • FilterChain
  • FilterConfig
MethodDescription
public void init(FilterConfig config)init() method is invoked only once. It is used to initialize the filter.
public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)doFilter() method is invoked every time when user request to any resource, to which the filter is mapped.It is used to perform filtering tasks.
public void destroy()This is invoked only once when filter is taken out of the service.

Filter Mapping in web.xml

<web-app>  

<filter>  

<filter-name>...</filter-name>  

<filter-class>...</filter-class>  

</filter>  

<filter-mapping>  

<filter-name>...</filter-name>  

<url-pattern>...</url-pattern>  

</filter-mapping>  

</web-app>

Example Code

HelloServlet.java

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;

public class HelloServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.print("<br>Welcome to servlet<br>");

}

}

MyFilter.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.*;

public class MyFilter implements Filter{

public void init(FilterConfig arg0) throws ServletException {}

public void doFilter(ServletRequest req, ServletResponse resp,

FilterChain chain) throws IOException, ServletException {

PrintWriter out=resp.getWriter();

out.print("Filter is invoked before");

chain.doFilter(req, resp);//sends request to next resource

out.print("Filter is invoked after");

}

public void destroy() {}

}

index.html
<a href="servlet1">Click here</a>

web.xml

<?xml version="1.0"?>

<web-app>

<servlet>

    <servlet-name>HelloServlet</servlet-name>

    <servlet-class>HelloServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>HelloServlet</servlet-name>

    <url-pattern>/servlet1</url-pattern>

  </servlet-mapping>

  <filter>

  <filter-name>f1</filter-name>

  <filter-class>MyFilter</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>f1</filter-name>

  <url-pattern>/servlet1</url-pattern>

  </filter-mapping>

</web-app>

Output

Output 1

Output 2


For more RTU V Sem Advance Java Lab Experiments Click here


3.3k questions

7.1k answers

395 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...