Frage

I have a servlet like this:


@WebServlet("/a/path")
@WebInitParam(name="name", value="name_value")
public class MyServlet extends HttpServlet {
//...

On this servlet I have put a filter:


@WebFilter(dispatcherTypes = { DispatcherType.REQUEST }, urlPatterns = { "/a/*" })
public class MyFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//...
       HttpServletRequest req = (HttpServletRequest)request;
       //problem comes here
       System.out.println(req.getServletContext().getInitParameter("name"));
//...
}

The problem is, that even if I set the @WebInitParameter in MyServlet, the programs prints out a null string (see the commented line //problem comes here in MyFilter). I verified and saw that init() method from servlet is executed before of doFilter(). So can anyone light me on this issue? Why the initParameter "name" is null, if it is set up to a value?

Thanks!

War es hilfreich?

Lösung

I think WebInitParam is defining init parameters for servlet and not for whole application context, so if you want acces parameters through ServletContext object, then define context params in you web.xml deployment descriptor.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top