//Home.html
<html>
<body>
<form method=post action="ser1">
<input type="submit" value="Invoke TestServlet using path /ser1"/>
</form><br><br>
<form method=post action="ser2">
<input type="submit" value="Invoke TestServlet using path /ser2"/>
</form>
</body>
</html>
Servlet Filter
<html>
<body>
<form method=post action="ser1">
<input type="submit" value="Invoke TestServlet using path /ser1"/>
</form><br><br>
<form method=post action="ser2">
<input type="submit" value="Invoke TestServlet using path /ser2"/>
</form>
</body>
</html>
//TestServlet.java
//TestServlet
package com.rajendra.servlets;
import javax.servlet.*;
import java.io.*;
public class TestServlet extends GenericServlet {
public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("In TestServlet, processing request<br/>");
}//service
}//class
//Creating MyFilter1.java
//Filter
package com.rajendra.servlets;
import javax.servlet.*;
import java.io.*;
public class MyFilter1 implements Filter {
public void init(FilterConfig fc){}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("In MyFilter1, filtering Request...<br/>");
fc.doFilter(req,res);
//Post Processing can be performed here if any
out.println("In MyFilter1, filtering Response...<br/>");
}//doFilter
public void destroy() {}
}//class
//MyFilter2.java
//Filter
package com.rajendra.servlets;
import javax.servlet.*;
import java.io.*;
public class MyFilter2 implements Filter {
public void init(FilterConfig fc){}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws ServletException, IOException {
PrintWriter out=res.getWriter();
out.println("In MyFilter2, filtering Request...<br/>");
fc.doFilter(req,res);
//Post Processing can be performed here if any
out.println("In MyFilter2, filtering Response...<br/>");
}//doFilter
public void destroy() {}
}//class
//Configuring the Filter Application
<!--web.xml-->
<web-app>
<filter>
<filter-name>filter1</filter-name>
<filter-class>com.santosh.servlets.MyFilter1</filter-class>
</filter>
<filter>
<filter-name>filter2</filter-name>
<filter-class>com.santosh.servlets.MyFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/ser1</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filter2</filter-name>
<servlet-name>ts1</servlet-name>
</filter-mapping>
<!--
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/ser2</url-pattern>
</filter-mapping>
-->
<servlet>
<servlet-name>ts1</servlet-name>
<servlet-class>com.santosh.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ts1</servlet-name>
<url-pattern>/ser1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ts1</servlet-name>
<url-pattern>/ser2</url-pattern>
</servlet-mapping>
</web-app>
Servlet Filter
No comments:
Post a Comment