A FilterChain is an instance given by the servlet holder to the designer giving a perspective into the summon chain of a separated solicitation for an asset. Filters utilize the FilterChain to summon the following filter in the chain, or if the calling filter is the last filter in the chain, to conjure the asset toward the end of the chain. Following is the syntax will be utilized in filters while using FilterChaining.
Click Here To Know More About FilterConfig
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
index.html
<html> <form action="./welcome"> username:<input type = "text" name = "userName"/></br></br> <input type = "submit" value="Login to SPlessons"/> </form> </html>
web.xml
<web-app> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>servletfilter.DemoServletFilters</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <filter> <filter-name>filter</filter-name> <filter-class>servletfilter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/welcome</url-pattern> </filter-mapping> </web-app>
Following is the web.xml pattern when using filters.
MyFilter.javapackage servletfilter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; public class MyFilter implements Filter { public void init(FilterConfig arg0) throws ServletException { //initialization of filter } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { PrintWriter out = response.getWriter(); String userName = request.getParameter("userName"); out.print("<h2>Hello " + userName + "</h2>"); chain.doFilter(request, response); //sends request to next resource out.print("<h2>Thank you " + userName + "</h2>"); } public void destroy() { //servlet filter destroy } }
public void init(FilterConfig config) is invoked only once it is used to initialize the filter. The doFilter technique for the Filter is called by the container every time a request/response pair is gone through the chain because of a client request for an asset toward the end of the chain. All filters are chained (in the order of their definition in web.xml). The chain.doFilter() is proceeding to the next element in the chain. The last element of the chain is the target resource/servlet. public void destroy() is invoked only once when filter is taken out of the service.
DemoServletFilters.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.*; public class DemoServletFilters extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print(" <h2>welcome to Splessons ServletFilters</h2> "); } }
Output
By compiling the code one can get the following output as shown below, enter the username and add button.
By clicking on the button following message will be displayed.
index.html
<html> <form action="./welcome"> <input type="submit" value="click here"/> </form> </html>
web.xml
<web-app> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>DemoFilterConfig</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <filter> <filter-name>filter</filter-name> <filter-class>MyFilterConfig</filter-class> <init-param> <param-name>user_name</param-name> <param-value>Enni</param-value> </init-param> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/welcome</url-pattern> </filter-mapping> </web-app>
MyFilterConfig.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; public class MyFilterConfig implements Filter{ FilterConfig config; public void init(FilterConfig config) throws ServletException { this.config=config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { PrintWriter out=response.getWriter(); String userName=config.getInitParameter("user_name");//get param-value from web.xml by call param-name out.print(" <h2>Hello "+userName+", your name is get from web.xml Filter tag</h2> "); chain.doFilter(request, response);//sends request to next resource out.print(" <h2>Thank you "+userName+"</h2> "); } public void destroy() {} }
In doGet(), the parameters are appended to the URL and sent along with the header information. Sets the content type of the response being sent to the client, if the response has not been committed yet.The given content type may include a character encoding specification.
DemoFilterConfig.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.*; public class DemoFilterConfig extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<h2>welcome to Splessons ServletFilterConfig</h2>"); } }
Output
Click on the button from the generated output.
Result will be as follows.
Web Server or HTTP Server is a server which is capable of handling HTTP request send by a client and respond back with a HTTP response.
Application Server or App Server can handle all application operations between users and an organization’s back end business applications or databases.It is frequently viewed as part of a three-tier application with: Presentaiton tier, logic tier,Data tier