How to execute Multiple filters
How to execute Multiple filters
I'm learning filters and I had a doubt. Say I have 4 filter classes f1,f2...f4. I want them to execute in sequence before the control being finally passed on to MyServlet servlet. I'm unable a create a proper mapping in web.xml.
Here's my code.
index.html The welcome page
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
<html>
<head>
<title>Home Page
</title>
</head>
<body>
<form action="???" method="post">
<input type="submit" value="Go to Next page" />
</form>
</body>
</html>
<html>
<head>
<title>Home Page
</title>
</head>
<body>
<form action="???" method="post">
<input type="submit" value="Go to Next page" />
</form>
</body>
</html>
f1.java
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
import javax.servlet.*;
import java.io.*;
public class f1 implements Filter
{
public void init(FilterConfig fg)
{
}
public void doFilter(ServletRequest req,ServletResponse res,FilterChain fn) throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
pw.println("Filter1 is working....................");
fn.doFilter(req,res);
}
public void destroy()
{
}
}
import javax.servlet.*;
import java.io.*;
public class f1 implements Filter
{
public void init(FilterConfig fg)
{
}
public void doFilter(ServletRequest req,ServletResponse res,FilterChain fn) throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
pw.println("Filter1 is working....................");
fn.doFilter(req,res);
}
public void destroy()
{
}
}
f2.java
//Has the same coding as above except the line pw.println("Filter2 is working....................");
The same goes for f3 and f4
Here is my MyServlet.java:-
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletRes