Get parameters of init filter in servlet

I have a filter that looks like this:

   <filter>
      <filter-name>TestFilter</filter-name>
      <filter-class>org.TestFilter</filter-class>
      <init-param>
         <param-name>timeout</param-name>
         <param-value>30</param-value>
      </init-param>
   </filter>

As we say ServletFilter and Servlets. In fact, I'm already in my servlet and executed the first part of doFilter. Therefore, the container must know the init parameter. I do not have access to change the Filter class.

Is it possible to get the value of the initialization parameter using the HttpServletRequest object?

The only solution I can think of is to read web.xml as a resource and try to find the value manually. But there seems to be a better solution.

+3
source share
2 answers

Why do you need this in your servlet? The filter parameter belongs to the filter. Your options:

  • init; , .
  • doFilter ( ) , .

.

web.xml:

  <context-param>
    <param-name>param1</param-name>
    <param-value>value</param-value>
  </context-param>

:

String paramValue = getServletContext().getInitParameter("param1");

, :

String paramValue = filterConfig.getServletContext().getInitParameter("param1");
+5

, . ,

public class MyFilter extends TheirFilter {
    public void init(javax.servlet.FilterConfig filterConfig) 
        throws javax.servlet.ServletException {
        super(filterConfig);
        // Retrieve the parameter here
    }
}

web.xml, .

0

Source: https://habr.com/ru/post/1718305/


All Articles