{"id":1706,"date":"2016-09-22T05:30:10","date_gmt":"2016-09-22T05:30:10","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1706"},"modified":"2016-09-22T05:53:09","modified_gmt":"2016-09-22T05:53:09","slug":"what-is-servlet-filter","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/what-is-servlet-filter\/","title":{"rendered":"What is Servlet filter"},"content":{"rendered":"<p><strong>What is Filter <\/strong><br \/>\nA filter is an object that is invoked at the preprocessing and postprocessing of a request.<\/p>\n<p>It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.<\/p>\n<p>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&#8217;t need to change the servlet.<\/p>\n<p><strong>Filter interface<\/strong><br \/>\nFilter interface contains three methods<\/p>\n<ol>\n<li><strong>public void init(FilterConfig config)<\/strong>init() method is invoked only once. It is used to initialize the filter.<\/li>\n<li><strong>public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)<\/strong>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.<\/li>\n<li><strong>public void destroy()<\/strong>This is invoked only once when filter is taken out of the service.<\/li>\n<\/ol>\n<p><strong>How to define a Filter<\/strong><br \/>\n<strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;web-app&gt;        \r\n\t&lt;filter&gt;  \r\n\t\t&lt;filter-name&gt;...&lt;\/filter-name&gt;  \r\n\t\t&lt;filter-class&gt;...&lt;\/filter-class&gt;  \r\n\t&lt;\/filter&gt;         \r\n\t&lt;filter-mapping&gt;  \r\n\t\t&lt;filter-name&gt;...&lt;\/filter-name&gt;  \r\n\t\t&lt;url-pattern&gt;...&lt;\/url-pattern&gt;  \r\n\t&lt;\/filter-mapping&gt;        \r\n&lt;\/web-app&gt;  \r\n<\/pre>\n<p><strong>Example of Filter<\/strong><br \/>\n<strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;web-app&gt;\r\n        &lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;AdminServlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;AdminServlet&lt;\/servlet-class&gt;\r\n\t&lt;\/servlet&gt;\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;AdminServlet&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;\/servlet1&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n\t&lt;filter&gt;\r\n\t\t&lt;filter-name&gt;f1&lt;\/filter-name&gt;\r\n\t\t&lt;filter-class&gt;MyFilter&lt;\/filter-class&gt;\r\n\t&lt;\/filter&gt;\r\n\t&lt;filter-mapping&gt;\r\n\t\t&lt;filter-name&gt;f1&lt;\/filter-name&gt;\r\n\t\t&lt;url-pattern&gt;\/servlet1&lt;\/url-pattern&gt;\r\n\t&lt;\/filter-mapping&gt;\r\n&lt;\/web-app&gt;  \r\n<\/pre>\n<p><strong>MyFilter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyFilter implements Filter \r\n{\r\n  public void init(FilterConfig arg0) throws ServletException \r\n  {\r\n  }\r\n\r\n  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)\r\n\tthrows IOException, ServletException {\r\n  PrintWriter out = resp.getWriter();\r\n  out.print(&quot;filter is invoked before&quot;);\r\n\r\n  chain.doFilter(req, resp);\/\/ sends request to next resource\r\n  out.print(&quot;filter is invoked after&quot;);\r\n}\r\n\r\n  public void destroy() \r\n  {\r\n  }\r\n}\r\n<\/pre>\n<p><strong>HelloServlet.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class HelloServlet extends HttpServlet \r\n{\r\n\tpublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException \r\n\t{\r\n\t\tresponse.setContentType(&quot;text\/html&quot;);\r\n\t\tPrintWriter out = response.getWriter();\r\n\t\tout.print(&quot;&lt;br&gt;welcome to servlet&lt;br&gt;&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>Whenever the <em>\/Servlet1<\/em>is accessed it will go through the filter before and after processing the actual java page  <em>HelloServlet.java<\/em><\/p>\n<p><strong>How to access Init param in filter<\/strong><br \/>\n<strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n&lt;filter&gt;\r\n\t&lt;filter-name&gt;f1&lt;\/filter-name&gt;\r\n\t&lt;filter-class&gt;MyFilter&lt;\/filter-class&gt;\r\n\t&lt;init-param&gt;\r\n\t\t&lt;param-name&gt;construction&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;no&lt;\/param-value&gt;\r\n\t&lt;\/init-param&gt;\r\n&lt;\/filter&gt;\r\n&lt;filter-mapping&gt;\r\n\t&lt;filter-name&gt;f1&lt;\/filter-name&gt;\r\n\t&lt;url-pattern&gt;\/servlet1&lt;\/url-pattern&gt;\r\n&lt;\/filter-mapping&gt;  \r\n.\r\n.\r\n<\/pre>\n<p><strong>MyFilter.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyFilter implements Filter \r\n{\r\n\tFilterConfig config;\r\n\r\n\tpublic void init(FilterConfig config) throws ServletException \r\n\t{\r\n\t\tthis.config = config;\r\n\t}\r\n\r\n\tpublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)\r\n\t\t\tthrows IOException, ServletException {\r\n\r\n\t\tPrintWriter out = resp.getWriter();\r\n\r\n\t\tString s = config.getInitParameter(&quot;construction&quot;);\r\n\r\n\t\tif (s.equals(&quot;yes&quot;)) {\r\n\t\t\tout.print(&quot;This page is under construction&quot;);\r\n\t\t} else {\r\n\t\t\tchain.doFilter(req, resp);\/\/ sends request to next resource\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void destroy() {\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Filter A filter is an object that is invoked at the preprocessing and postprocessing 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&hellip; <a href=\"https:\/\/codethataint.com\/blog\/what-is-servlet-filter\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1706","post","type-post","status-publish","format-standard","hentry","category-servlets"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1706"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1706\/revisions"}],"predecessor-version":[{"id":1711,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1706\/revisions\/1711"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}