Hi Any body please tell me how to work with servlet filters and can u post smple application to search webpage based on input values?
Hi venkatash, In servlets the filters are used to develop the frientend controller design patterns. It mean, wehenever we send request to server with any url, first the request go through the fileter. According the servlet specification the filters are used for to store the loggin information,for authentication (checking get or post request),image convertion , data compression etc. The filters contain the url like. ex: authentication filers: success.jsp: <html> <body> we got success message </body> </html> error.jsp: ---------- get request is restricted. AuthenticationFilter.java: public class AuthentictionFilter implements Filter { public void init(FilterConfig config) { } public void doFilter(ServletRequest request,ServletResponse response, FilterChain cahin) { HttpServletRequest request1=(HttpServletRequest)request; String method=request1.getMethod(); if(method.equalsIgnoreCase("post")) { chain.doFilter(request,response); }else { RequestDispatcher dis=request.getRequestDispatcher("error.jsp"); dis.forward(request,response); } } public void destroy() {} } compile this one and palce in classes folder. web.xml: -------- <web-app> <filter> <filter-name>af</filter-name> <filter-class>AuthenticationFilter</filter-class> </filter> <filter-mapping> <filter-name>af</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> description: ------------- for filters no need to use <load-on-startup> tag. At time of > deployment the server create the > filter object and call the > init(FilterConfig) method. whenever we > send the request to server like > http://localhost:8080/filterproject/success.jsp > , first the server call the > doFilter(ServletRequest,ServletResponse) > method . This method check the request > get or post. If the request mthod is > get it forward the request to > error.jsp. If the request method id > post the > chain.doFilter(request,response) send > the request to success.jsp.
Ads