Filter is used for pre-processing of requests and post-processing of responses.

Filter Api- it’s a part of servlet api, filter interface can be found in javax.servlet package.

Life cycle-
1- init
2- dofilter
3- destroy

Filterchain interface- its used to invoke other filters in the chain.

Example 11- Web app to show pre-processing text before request is handled by servlet and post-processing text after response is generated by servlet.

output-

zxc

Project Structure-

mlp

3 files-

1-index.html(inside WebContent)

2-Firstfil.java(inside pacage=’ab’)

3-FirstServ.java(inside pacage=’ab’)

4-SecFil.java(inside pacage=’ab’)

5-web.xml(inside WEB-INF)

1-index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServ">
<input name="name">
<input type="submit" value="ok">
</form>
</body>
</html>

2-Firstfil.java(filter)


package ab;

import java.io.*;
import javax.servlet.*;

public class Firstfil implements Filter {

public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String fs=request.getParameter("name");
System.out.println("first filter "+fs);
chain.doFilter(request, response);
System.out.println("after first filter chain-dofilter");
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

}

3-FirstServ.java(servlet)


package ab;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServ extends HttpServlet {
private static final long serialVersionUID = 1L;

public FirstServ() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String fs=request.getParameter("name");
response.getWriter().print(fs);
System.out.println("first servlet "+fs);
// pass the request along the filter chain
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

4-SecFil.java(filter)


package ab;

import java.io.*;
import javax.servlet.*;

public class SecFil implements Filter {

public void destroy() {
// TODO Auto-generated method stub
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String fs=request.getParameter("name");
System.out.println("second filter "+fs);
// pass the request along the filter chain
chain.doFilter(request, response);
System.out.println("after second filter chain-dofilter");
}

public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

}

5-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Filters</display-name>
<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>
<filter-name>a</filter-name>
<filter-class>ab.Firstfil</filter-class>
</filter>
<filter-mapping>
<filter-name>a</filter-name>
<url-pattern>/FirstServ</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>b</servlet-name>
<servlet-class>ab.FirstServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/FirstServ</url-pattern>
</servlet-mapping>

<filter>
<filter-name>c</filter-name>
<filter-class>ab.SecFil</filter-class>
</filter>
<filter-mapping>
<filter-name>c</filter-name>
<url-pattern>/FirstServ</url-pattern>
</filter-mapping>
</web-app>

Advertisements