In this article, I am going to share Servlet Filter Interview Questions and Answers frequently asked on Interview. You can expect many questions from this topic in the interview.
Q1. What is the filter?
Ans: A filter is a web component like Servlet. Servlet filter is a java program that implements javax.servlet.filter interface.
Q2. What is the use of Servlet Filter?
Ans: Java Servlet filter is used to intercept the request and do some pre-processing and can be used to intercept the response and do post-processing before sending to a client in a web application.
Q3. What is the task of Filter?
Ans: It mainly does two tasks, namely:
- Pre-Processing Task
- Post-processing Task
Q4. What are the lifecycle methods of the Filter and explain their uses?
Ans: Filter interface provides the life cycle methods for a filter.
|public void init(FilterConfig config)||init() method is invoked only once. It is used to initialize the filter.|
|public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)||doFilter() method is invoked every time when a user request to any resource, to which the filter is mapped. It is used to perform filtering tasks.|
|public void destroy()||This is invoked only once when a filter is taken out of the service.|
Q5. Explain the filter uses and it’s working?
- Servlet is responsible for core request processing. Before sending the request to a servlet. You may want to perform some tasks which are called as pre-processing tasks.
- After request processing is completed by Servlet, you may want to perform some tasks which are called as post-processing tasks.
- If you write the code for the pre-processing tasks and post-processing tasks across all the servlets code gets duplicated and gives the maintenance problem when you try to change that code.
- To avoid the code duplication problem and maintenance problem, you need to write the pre-processing tasks and post-processing tasks code in a centralized place called Filter.
Q6. What are the various pre-processing tasks which you can perform on incoming request before core request processing?
Ans: Following are various pre-processing tasks which you can perform on incoming request before core request processing.
- Security checks include Authentication and Authorization.
- Verifying Session Validity etc.
Q7. What are various post-processing tasks which you can perform on the outgoing response after core request processing?
Ans: Following are various post-processing tasks which you can perform on the outgoing response after core request processing.
- Data Compression
- Data Encryption/ Encoding etc
Q8 What are the steps to develop the filter interface?
- Write your own filter class by implementing Filter interface which is in Javax.servlet package.
- Your filter class has to override the following 3 lifecycle methods
- init(FilterConfig fC): This method will be called at the time of creating the filter object.
- doFilter(Servlet.req, Servlet.res, FilterChain fC): This method will be called when the client will send the request, a container will verify to invoke the filter for the current request.
- Destroy(): Call to destroy the filter.
3. Configure the filter in the web.xml as follows
Q9. What is the advantage of Java Servlet filter?
- A filter is pluggable.
- One filter doesn’t have a dependency on another resource.
- Less Maintenance
Q10. What is Filter Chaining?
Invoking Multiple Filters one by one as a chain is called as Filter Chaining. Filter chain will be managed by web-container automatically based on the order of filters configured in web.xml.You can add or remove the filters from the filter chain by changing web.xml. You can change the order of filters in the filter chain just by changing the filter configuration in web.xml.
Q11. What is Servlet Chaining?
Invoking multiple servlets (invoking service()) one by one as a chain is called as Servlet chaining. Servlet chain will be managed by the developer by writing the code in the servlet class by using request dispatcher (programming approach) You can add or remove the servlets from servlet chain by changing the code of Servlet. You can change the order of Servlets in servlets in servlet chain just by changing the code inside a servlet.
Q12. What are the interfaces of Filter API?
Ans: Filters have their own API the javax.servlet package contains the three interfaces of Filter API.
2. Filter Chain
Q13. What is the method of the Filter Chain interface?
Ans: The FilterChain interface contains only one method:
Public void doFilter (HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource.
Q15. What is the FilterConfig?
Ans: An object of FilterConfig is created by the web container. This object can be used to get the configuration information and get init parameters values of the filter program from the web.xml file.
Q14. What are the methods of FilterConfig Interface?
Methods of FilterConfig interface
There are the following 4 methods in the FilterConfig interface.
1. public void init (FilterConfigconfig): init () method is invoked only once it is used to initialize the filter.
2. public String getInitParameter (String parameterName): Returns the parameter value for the specified parameter name.
3. public java.util.EnumerationgetInitParameterNames (): Returns an enumeration containing all the parameter names.
4. public ServletContext getServletContext (): Returns the ServletContext object.
Q15. What is the difference between dofilter() method of filter interface and dofilter() method of FilterConfig interface?
Ans: Dofilter() method of filter interface is life cycle method of servlet filter program so programmer uses this method to place pre-request processing and post response generation logic.
Dofilter () method of filter chain interface is not a life cycle method so a programmer uses this method to invoke the next filter in the chain to mapped servlet or JSP program.
That’s all about Servlet Filter Interview Questions and Answers from Core Java Interviews. I have covered almost everything as per my knowledge for both freshers and experienced with 0 to 5 years, so prepare well for the Java interview Questions, I suggest you take a look on more Java Programming Interview Questions with answers on different topics of the core java posted in this xadmin website. Keep Learning! All the Best!